How do I match the logged in user to his/her member name from the DB and use echo to display it on the page they are visiting?

Hi, new to the forum! I have a simple PHP question that I couldn’t seem to find a sufficient answer for by searching. I have a small MySQLi DB and want to retrieve something from “members”, based on which user is currently logged on. Something like “Hi member!” but replacing “member” with the matching logged in user’s name (member_name) from the DB. I know about “select” and all, but I just can’t figure it out. Any help would be most appreciated!

Please go easy on me, I’m a total PHP noob. :smile:

1 Like

Generally, when a user logs in you would set a session variable containing the data you want to display in the app and then just echo that variable where you want it displayed.

This only applies to something you would want to persistently display such as the persons name. Other data would be queried as needed.

1 Like

Okay thanks! So I added this to my login code:

$_SESSION[‘member_name’] = $username;

How do I echo it? I tried: <?php echo $username; ?>, <?php echo $member_name; ?>, and <?php echo "$member_name;" ?> and none is working. :frowning:

Where are these variables magically coming from? If your using a Database the values would come from the query result. If you set the session properly you would echo the session.

$_SESSION['member_name'] = $row['username'];

echo "Hello {$_SESSION['member_name']}";

It’s not working. :frowning: Here’s the snippet from my account creation code:

if (count($errors) == 0) {
$password = password_hash($password, PASSWORD_BCRYPT);

$query = "INSERT INTO members (member_name, member_email, member_password) 
		  VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['member_name'] = $row['member_name'];
header('location: thanks.html');

}

I said login, not account creation.

Also, NEVER EVER put variables in your query. You need to use Prepared Statements.

1 Like

Speaking of variables and prepared statements, I changed the mentioned code to:

// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = password_hash($password, PASSWORD_BCRYPT);

  $sql = "INSERT INTO members (name, password, email) VALUES (?, ?, ?);
if($stmt = mysqli_prepare($db, $sql)){
// Bind variables to the prepared statement as parameters
  mysqlstmt_bind_param($stmt, "sss", $member_name, $member_passwored, $member_email);
$username = "member_name";
$password = "member_password";
$email = "member_email";

  mysqli_stmt_execute($stmt);

// mysqli_query($db, $query);
// $_SESSION['member_name'] = $row['members'];
header('location: thanks.html');

}

And now I broke the script. :frowning: Help please!

Nobody is going to know what that means.

I would highly recommend you use PDO instead of mysqli.

Yes, I tried to use prepared statements in the code I showed, and it doesn’t work, this is the code snippet in reference:

if (count($errors) == 0) {
$password = password_hash($password, PASSWORD_BCRYPT);

  $sql = "INSERT INTO members (name, password, email) VALUES (?, ?, ?);
if($stmt = mysqli_prepare($db, $sql)){
// Bind variables to the prepared statement as parameters
  mysqlstmt_bind_param($stmt, "sss", $member_name, $member_password, $member_email);
$username = "member_name";
$password = "member_password";
$email = "member_email";

  mysqli_stmt_execute($stmt);

// mysqli_query($db, $query);
// $_SESSION['member_name'] = $row['members'];
header('location: thanks.html');

}

If this is not correct, then how do I fix it? It simply produces a blank screen.

I would suggest you take a look at the countless scripts and tutorials that are out there and actually learn about what you are trying to do. It’s pretty clear you are just throwing stuff out hoping it sticks to the wall without understanding what you are doing.

Your OP says you want to " retrieve something from “members", based on which user is currently logged on." yet you keep posting code related to registering a user. You arent “retrieving” anything.

1 Like

My bad, I am working on two things at once. Anyway, I got it working with PDO! Thanks for your help!! :slight_smile: