Vani Kalapciev Vani Kalapciev - 3 months ago 8
MySQL Question

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

I am trying to retrieve the user's username form the database. I am searching the user from

$_SESSION["ID"]
, which is set after the user successfully logs into the site.

When I am trying to display the username it displays the
$_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>


Do you have any idea what is going on and how to fix it?

Display
MySQL table

Answer

// Please try below code and I hope this will help you.

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