0

More specifically I can't get the $last_name to display at all.

In the following code:

$name_and_date = "$first_name $last_name | $today | <a href=\"logout.php\">Log out</a>";

Here is more of the code.

// Place Session variables into local variables
$user_id = $_SESSION['user_id'];
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$registration_date = $_SESSION['registration_date'];
// Convert the sign up date to be more readable by humans
$registration_date = strftime("%b %d, %Y", strtotime($registration_date));
// Use PHP to find today date and get it ready for display
$today = date("F j, Y");
$name_and_date = "$first_name $last_name | $today | <a href=\"logout.php\">Log out</a>";
require_once ('../mysqli_connect.php'); // Connect to the db.
// Query member data from the database and ready it for display
    $mysqli = new mysqli("localhost", "root", "", "sitename");
    $dbc = mysqli_query($mysqli,"SELECT * FROM users WHERE user_id='$user_id'");
if (!$dbc) {
    // There was an error...do something about it here...
    print mysqli_error();
}  else {
    while($row = mysqli_fetch_array($dbc)){ 
        $city = $row["city"];
        $zip = $row["zip"]; 
        $bio_body = $row["bio_body"];
        $last_name = $row["last_name"];
    }
}
2
  • Soy-bean, no offence but this code is not a beauty. If this is not your code but something you're learning from, dump it and take up something better! This is not a very neat piece of code, to say the least. Commented Sep 29, 2009 at 18:48
  • What do I mean with that: Badly formatted. Passing of many single session vars to local vars = code smell: wrong use of session. String building without concatenation, especially when building queries = suboptimal practice. Requires in the middle of the code. ... etc. Commented Sep 29, 2009 at 18:56

4 Answers 4

1

The best thing you can do in these cases is to check your code with a debugger. I recommend using Netbeans IDE with the PHP extension, very easy to set up and debug. Then run your code step by step and keep an an eye on the values of both the $_SESSION variable and the $first_name variable.

Once you get used to debugging you never go back...

Sign up to request clarification or add additional context in comments.

Comments

0

It's probably empty. do:

<?php var_dump($last_name);?>

Check where you assigned $_SESSION['last_name'] there may be a typo. Check the row in the database, see if that field was saved properly.

1 Comment

It says NULL but when I call the last name like this for example $last_name = $row["last_name"]; the last name is displayed?
0

Have you called session_start() at the beginning of the script? Sessions don't work unless you call that function.

2 Comments

do you include session_start() in the beginning of every page?
its included in the header which is included in every page.
0

i dont see the code where the values are getting assigned to variables .. as per your comment....

"It says NULL but when I call the last name like this for example $last_name = $row["last_name"]; the last name is displayed?"

i am sure ur doing $row["last_name"]; inside the while($row = mysqli_fetch_array($dbc)){

its available there , thats why it show.. however its not automatically assigned to the session. please post the code where u assigned $row["last_name"] to $_SESSION['last_name']

2 Comments

I add it to the code just look at the very bottom to see what I did it looks like this $last_name = $row["last_name"];
and why do you think it would be automatically available in $_SESSION['last_name'] ? cause u just did a local assignment $last_name = $row["last_name"]; u need something like $_SESSION['last_name'] = $row['last_name']; and this should show the next time u refresh the page.