在用户登录时接收用户的名字

时间:2015-01-15 03:17:10

标签: php

我使用this website实现了一个基本且简单的登录方法。我可以成功登录,但我想检索登录网站的成员的名字,以将其存储到$_SESSION对象中。

$sql="SELECT * FROM users WHERE email='$myemail' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
 $count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
     //$_SESSION[];
    //  header("Location:http://localhost/website/test.php");
     }
     else 
         echo "Wrong Username or Password";

Reference to the table

2 个答案:

答案 0 :(得分:1)

email字段必须包含唯一条目。

请参阅id-4& id-4行包含完全相同的数据。 但至于你的答案,

if($count==1)
{
$row = mysql_fetch_assoc($result);
$f_name = $row['firstname'];
}

现在,如果email=cf@hotmail.compassword=g您的查询将失败,因为$count将不会是1。因此,有效用户也会看到登录失败消息。

答案 1 :(得分:1)

这是你想要做的吗?

if($count==1)
{        
     $row = mysql_fetch_row($result);

     session_start();
     $_SESSION['first_name'] =  $row['firstname'];

     header("Location:http://localhost/website/test.php");
}
相关问题