$ _Session没有收集所有用户信息?

时间:2014-02-10 13:02:11

标签: php mysql database session

目前在用户网站上工作并遇到一些问题:

 <?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>";

?> 

这似乎正确地显示了大部分信息,名字,姓氏和电子邮件地址有效。当我尝试显示照片名称,用户名或使用用户信息输入的日期时,它不会显示并显示为空白?有没有人有任何想法我可能做错了什么?

1 个答案:

答案 0 :(得分:2)

您在开始会话之前使用会话

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

应该是

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

根据用户名,您正在为该用户提取记录,因此无需显示会话变量中的信息,您可以直接显示如下

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>";