2014-04-01 138 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. 
}