2014-04-01 101 views
0

我试图创建一个页面,检查它是否正确登录信息。出于某种原因,当我回应从数据库选择数据的结果查询时,我没有从数据库中获取数据,我不确定它是什么数据。下面是显示在页面上:PHP/MySQL查询返回不在数据库中的结果

Connected successfully 
Resource id #3 

这里是我的PHP代码:

<?php 
//start session 
session_start(); 

//connect to MySQL Database 
$con = mysql_connect('localhost', 'root', ''); 
if (!$con) { 
    die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully<br />'; 

// make members the current db 
$db_selected = mysql_select_db('members_db', $con); 
if (!$db_selected) { 
    die ('Can\'t use members database : ' . mysql_error()); 
} 


$email = $_SESSION['email']; 

$result = mysql_query("SELECT email,password FROM `members` WHERE 'email' = '$email'"); 
if (!$result) { 
    echo 'Could not run query: ' . mysql_error(); 
    exit; 
} 
$row = mysql_fetch_row($result); 

if ($email == $row[0] && $_SESSION['password'] == $row[1]) 
{ 
    $_SESSION['loggedin']="YES"; 
    $url= "Location: welcome.php"; 
    header($url); 
    exit; 
} 

echo $result; 

/* 
//Problems: 
$problem = ""; 
if ($email == $row[0] && $_SESSION['password'] != $row[1]) 
{ 
    $problem = "invalidPassword"; 
} 
if ($email != $row[0]) 
{ 
    $problem = "InvalidUser"; 
} 
$url = "Location: index.php?problem=$problem"; 
header($url); 
exit;*/ 
?> 
+1

mysql_query返回一个资源,所以这就是您在回显时获得的资源。你应该看看'$ row'中的内容,它会告诉你为什么你的失败 –

+0

必须使用'mysql'是折旧使用'mysqli' – Chitowns24

+0

尝试'echo $ row [0]; $ row = mysql_fetch_row($ result);'后的echo $ row [1]''。还要确保你的查询返回一行。您可以使用['mysql_num_rows'](http://php.net/manual/en/function.mysql-num-rows.php)查找返回的行数。 – Uours

回答

0

这里,如果这有助于检查,如果你得到正确的数据,它看起来并不像你甚至还让进入验证用户的正确区域

$row = mysql_fetch_row($result); 
if ($email == $row[0] && $_SESSION['password'] == $row[1]) 
{ 

$_SESSION['loggedin']="YES"; 
$url= "Location: welcome.php"; 
header($url); 
exit; 
} 
else //Validation failed 
{ 

echo $row[0] . ' SESSION EMAIL ' . $email; //Which would echo the email that was returned and the email stored in the session, if they do not match it means the password was wrong. 
}