0

I'm trying to display a users information on a profile page after they get logged in. Here is the code I'm using:

<?php require_once('connection.inc.php'); $conn = dbConnect('read'); // get the username's details from the database $sql = ("SELECT firstname, lastname FROM members WHERE username ='".$_SESSION['username']."'"); $_SESSION['firstname'] = 'firstname'; $_SESSION['lastname'] = 'lastname'; // initialize and prepare statement $stmt = $conn->stmt_init(); $stmt->prepare($sql); // bind the result, $stmt->bind_result($_SESSION['firstname'], $_SESSION['lastname']); $stmt->execute(); $stmt->fetch(); 

The code runs without any errors but on the profile page the results get displayed as: firstname: firstname lastname: lastname

instead of plugging in the information that was supposed to be pulled from the database.

The display code on the profile page itself is correct, because I can set the session variables above from the user login page, and they work properly. I just don't want to be pulling in all that information at login, if the user isn't going to be doing anything with it. Thanks for any help.

2 Answers 2

1

Citation from the php manual:

Note that all columns must be bound after mysqli_stmt_execute() and prior to calling mysqli_stmt_fetch(). Depending on column types bound variables can silently change to the corresponding PHP type.

see http://php.net/manual/en/mysqli-stmt.bind-result.php

So just put it that way:

$stmt->execute(); $stmt->bind_result($_SESSION['firstname'], $_SESSION['lastname']); $stmt->fetch(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip on order, but I still get the same result.
0

Okay, there are a number of issues regarding your code.

As artiifix already mentioned, you can only bind the results after you've executed the query. It is, however, better to use prepared statements using parameters.

Also, you set $_SESSION['firstname'] to firstname and $_SESSION['lastname'] to lastname, which is why you get that output. You can remove those lines of code.

<?php require_once('connection.inc.php'); $conn = dbConnect('read'); // get the username's details from the database $sql = "SELECT firstname, lastname FROM members WHERE username=?"; /* See this? I've put a parameter in the query. * You need to do this in order to prepare a statement. */ // initialize and prepare statement $stmt = $conn->stmt_init(); $stmt->prepare($sql); // Bind parameters and execute the query $stmt->bind_param("s", $_SESSION['username']); $stmt->execute(); // Next, bind the results to the variables. $stmt->bind_result($_SESSION['firstname'], $_SESSION['lastname']); $stmt->fetch(); [...] 

Furthermore, if above code is the top of your php-script, you would need to open the session to access the session variables!

<?php session_start(); require_once('connection.inc.php'); $conn = dbConnect('read'); [...] 

And, to simplify things even more: if the user is already logged in, and the session variables are already set, there is no need to fetch them again.

<?php session_start(); echo $_SESSION['firstname'] . ' ' . $_SESSION['lastname']; [...] 

10 Comments

I tried the code above and now the results are blank. I'm confused as to why my original code, which I ripped from my login page (where it does work to assign the session variables) wouldn't work on another page. Thanks for the help.
I missed something... session_start() is not called in this script, so session variables aren't accessible.
OK. That makes sense. Please excuse the newb question, but if I start a new session does that mess with variables that were set in the previous session? Thanks again for helping me out.
Not at all. session_start() just initialises the session handler. All previously saved session variables are kept, until the session is destroyed using session_destroy().
Maybe a better question would be if it's bad programming to set all the session variables pertaining to the user information at the login screen, even though the user more then likely won't use any of it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.