使用会话变量从数据库中检索数据

时间:2014-01-14 07:50:53

标签: php

<?php

{
session_start();
include "dbconnect.php";

$email = $_SESSION['email'];
echo $email;
$query = "SELECT uid FROM master WHERE emailid = '$email' ";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
while($row)
 {
  $uid=$row[1];

  echo $uid;

   }

} 这个code.it是什么错误给了电子邮件,但无法使用会话变量电子邮件检索uid。 请帮忙

4 个答案:

答案 0 :(得分:3)

while($row) {
   $uid = $row[1];
   echo $uid;
}

应该是

while($row) {
   $uid=$row['uid'];  
   echo $uid;    
}

尝试此代码是否适合您

session_start();
include "dbconnect.php";

$email = $_SESSION['email'];
//echo $email;
$query = "SELECT uid FROM master WHERE emailid = '$email' ";
$result = mysql_query($query);

while($row=mysql_fetch_array($result)){
    $uid=$row['uid'];
    echo $uid;
}

答案 1 :(得分:0)

我不知道为什么你的代码开头有{,你应该删除它。第二件事,删除echo $email;,你应该有工作代码

答案 2 :(得分:0)

尝试$ uid = $ row [0]而不是$ uid = $ row [1]; 因为$ row是一个数组.Array总是以0的形式启动它。 在这里你定义了$ uid = $ row [1]这意味着它将显示结果集第二行的结果。但是没有第二行只有一行是“uid”所以试试$ uid = $ row [0]而不是$ uid = $ row [1];

答案 3 :(得分:0)

试试这个:

<?php
session_start();
include "dbconnect.php";

$email = $_SESSION['email'];

$query = "SELECT uid FROM master WHERE emailid = '".$email."'";
$result = mysql_query($query);
if(mysql_num_rows($result)>0)
{
    $row = mysql_fetch_array($result);
    $uid = $row["uid"];
    echo $uid;
}
else
{
    echo "No record found";
}
?>