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

When reading from MYSQL only displays $_SESSION["ID"];

我正在尝试从数据库中检索用户的用户名。我是从 $_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

试试这个:

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

而不是 bindValue

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

<?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>