tags:

views:

57

answers:

4

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"];
 }
}
A: 

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.

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

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

Byron Whitlock
yes i include the session_start()
do you include session_start() in the beginning of every page?
tharkun
its included in the header which is included in every page.
A: 

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']

Sabeen Malik
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.
Sabeen Malik
+1  A: 

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

Alex