从MYSQL读取时只显示$ _SESSION [" ID"];

时间:2016-08-17 13:43:03

标签: php mysql session session-variables

我正在尝试从数据库中检索用户的用户名。我正在从$_SESSION["ID"]搜索用户,该用户是在用户成功登录网站后设置的。

当我尝试显示用户名时,会显示$_SESSION["ID"]

 <?php
include ("db_safe.php");

$user_ID = $_SESSION["ID"];

$stmt_user_profile = $dbh->prepare("SELECT * FROM users WHERE user_ID LIKE :user");
$stmt_user_profile->bindValue(':user', $user_ID);
$stmt_user_profile->execute();

$result_user_profile = $stmt_user_profile->fetch(PDO::FETCH_ASSOC);

foreach ($result_user_profile as $user_info){
    $profile_username = $user_info["username"];
    $profile_email = $user_info["email"];
}
?>

<h3>Hello <?php echo $profile_username; ?> </h3>

你知道发生了什么以及如何解决它吗?

Display MySQL table

2 个答案:

答案 0 :(得分:0)

尝试使用:

$stmt_user_profile->bindParam(':user', $user_ID, PDO::PARAM_STR);

而不是 bindValue

答案 1 :(得分:0)

//请尝试以下代码,我希望这对您有帮助。

<?php
include ("db_safe.php");
$user_ID = $_SESSION["ID"];
$stmt_user_profile = $dbh->prepare("SELECT * FROM users WHERE user_ID LIKE ?");
$stmt_user_profile->execute(array($user_ID));
$allusers = $stmt_user_profile->fetchAll();

foreach ($allusers as $user)
{
     $profile_username = $user["username"];
     $profile_email = $user["email"];
}

?>

<h3>Hello <?php echo $profile_username; ?> </h3>