简体   繁体   中英

$_Session not gathering all user information?

Working on a user website at the moment and having some trouble with this code:

 <?php 

 // Connects to your Database 
mysql_connect ("host","a2530897_admin","pass") or die("Could not conenct");
mysql_select_db("a2530897_members") or die ("could not connect to dadabase!");

$SQL=mysql_query("SELECT * FROM users WHERE username='".$_SESSION['username']."'") 
or die(mysql_error());
session_start();

echo "<strong>First Name: ". $_SESSION['first_name']."</strong>";
echo "<strong>Last Name: ". $_SESSION['last_name']."</strong>";
echo "<strong>Email Name: ". $_SESSION['email_address']."</strong>";
echo "<strong>Photo: ". $_SESSION['photo']."</strong>";
echo "<strong>Username: ". $_SESSION['username']."</strong>";
echo "<strong>Date: ". $_SESSION['date3']."</strong>";

?> 

This seems to bring up most of the information correctly, first name, last name and email address works. When I try to bring up the photo name, username or the date that was entered with the user information, it doesn't appear and comes up blank? Does anyone have any ideas what I might be doing wrong?

You are using Session before start session

$SQL=mysql_query("SELECT * FROM users WHERE username='".$_SESSION['username']."'") 
or die(mysql_error());
session_start();

should be

session_start();
$SQL=mysql_query("SELECT * FROM users WHERE username='".$_SESSION['username']."'") 
or die(mysql_error());

As based on username you are fetching record for that user then no need to display information from session variable you can directly display like below

session_start();
$SQL=mysql_query("SELECT * FROM users WHERE username='".$_SESSION['username']."'") 
or die(mysql_error());

$row = mysql_fetch_array($SQL);

echo "<strong>First Name: ". $row['first_name']."</strong>";
echo "<strong>Last Name: ". $row['last_name']."</strong>";
echo "<strong>Email Name: ". $row['email_address']."</strong>";
echo "<strong>Photo: ". $row['photo']."</strong>";
echo "<strong>Username: ". $row['username']."</strong>";
echo "<strong>Date: ". $row['date3']."</strong>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM