2010-12-02 78 views
0

我想查看登錄用戶是否已通過驗證。但有一個錯誤:PHP - Mysql查詢問題

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/psmcouk/public_html/colemansystems/verify.php on line 332

這裏是PHP代碼:

$user1 = $_SESSION['usr']; 

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName=$user1"); 

while($row = mysql_fetch_array($result)) //LINE 332 
    { 
    $valid = $row['valid']; 

    } 
    if($valid == "1"){ 
     echo "$user1, you're account is currently verified."; 

    } 

我看不出有什麼不對這個代碼。

謝謝!

回答

2

以上所有答案都是蹩腳的。

$user1 = mysql_real_escape_string($_SESSION['usr']); 
$query = "SELECT valid FROM phpbb_members WHERE memberName='$user1' and valid=1"; 
$result = mysql_query($query) or trigger_error(mysql_error()." in ".$query); 
$valid = mysql_num_rows($result); 
if($valid){ 
    echo "$user1, your account is currently verified."; 
} 
+0

非常感謝!第一次工作!非常感激! – ryryan 2010-12-02 11:56:18

1

嘗試使用:

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName='$user1'") 
      or die(mysql_error()); // to get if any error exists 
1

我猜$用戶應該被引用:

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName='$user1'"); 

你總是可以看到什麼錯我將回聲mysql_error();查詢

2

你可能有後一個SQL錯誤。嘗試

if (!$result) { 
    echo 'Invalid query: ' . mysql_error() . "\n"; 
} 
-1

你應該使用它之前測試的mysql_query的結果,如果按照從php.net的例子:

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName=$user1"); 
if (!$result) { 
    die('Request problem : ' . msql_error()); 
} 

while($row = mysql_fetch_array($result)) //LINE 332 
... 
1

由於已經發布,你就必須把用戶名在單引號:

$query = "SELECT * FROM phpbb_members WHERE memberName = '".$user1."'"; 

假設用戶名列是varchar。您使用的代碼僅在比較數字時纔有效,例如整數。

一般性評論:根據數據庫列的大小,選擇特定行可能是合理的,而不是全部使用*。例如:

$query = "SELCT memberName, valid FROM phpbb_members"; 
1
$user1 = $_SESSION['usr']; 

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName=$user1"); 

while($row = mysql_fetch_field($result)) //LINE 332 
    { 
    $valid = $row['valid']; 

    } 
    if($valid == "1"){ 
     echo "$user1, you're account is currently verified."; 

    } 

嘗試。