我正在尝试从我的数据库中获取当前登录的用户信息。但是当我运行我的代码并以跟踪用户帐户登录时,它不会显示他们的信息,但如果我登录到我数据库顶部的用户,它会显示他们的信息。示例:
Bigfella 帐户实际上从数据库中获取信息,例如:个人资料图片、描述。
Beta 帐户从 bigfella 帐户获取个人资料图片和描述
这是我的数据库表:
这是我的代码:
<?php
include('functions.php');
if (!isLoggedIn()) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}
require_once 'dbcon.php';
?>
<?php
$stmt = $DB_con->prepare('SELECT id, username, description, userprofile FROM users ORDER BY id DESC');
$stmt->execute();
if($stmt->rowCount() > 0)
{
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
}
?>
页面的 HTML:
<!DOCTYPE html>
<html>
<head>
<title> Home</title>
</head>
<body>
<!-- logged in user information -->
<div class="profile_info">
<img src="uploads/<?php echo $row['userprofile']; ?>" class="img-rounded" width="250px" height="250px" />
<div>
<?php if (isset($_SESSION['user'])) : ?>
<strong><?php echo $_SESSION['user']['username']; ?></strong>
<small>
<i style="color: #888;">(<?php echo ucfirst($_SESSION['user']['user_type']); ?>)</i>
<br>
<a href="home.php?logout='1'" style="color: red;">logout</a>
</small>
<h4> Status: <?php echo $description ?> </h4>
<?php endif ?>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
关于如何解决这个问题的任何想法?所以它只获取登录用户信息?先感谢您!!
最佳答案
看起来您可以像这样运行查询:
$stmt = $DB_con->prepare('SELECT id, username, description, userprofile FROM users WHERE id='.$_SESSION['user']['id']);
关于php - 试图从 mysql 数据库中检索登录用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47877210/