2010-09-18 112 views
0

可能重複:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in selectPHP(MySQL的)錯誤: 「警告:mysql_num_rows()預計參數1是資源」

if (!empty($_POST)){ 

    $email_to=$_POST['email_to']; 
    $email_to=mysql_real_escape_string($_POST['email_to']); 

    $sql = "UPDATE `cosmos`.`members` SET `conf` = '2' WHERE `members`.`email` = '$email_to';"; 
    $result=mysql_query($sql) or trigger_error(mysql_error().$sql); 

    $count=mysql_affected_rows($result);     // line 20 
    if($count==1){ 

    $rows=mysql_fetch_array($result); 
    $unique=$rows['u_code']; 
    $name=$rows['username']; 
    // ---------------- SEND MAIL FORM ---------------- 
    $to=$email_to; 
    $subject="Your Account Password Request! - Cosmos"; 
    $header="from: Tayal's/Cosmos <[email protected]>"; 
    $messages= "Hey $name ,\r\n"; 
    $messages.="You recently requested a new password"; 
    $messages.="<br /><a href='confirm.php?uid" . $unique . "'>Confirmation Link</a> \r\n"; 
    $sentmail = mail($to,$subject,$messages,$header); 
    echo $messages; 
    } else { 
    echo "Not found your email in our database"; 
    } 


} 

警告:mysql_affected_rows()函數需要參數1是資源,布爾在C:\ wamp \ www \ a \ l \ forget.php在線20給出

+1

''conf' = \'2 \''是個問題。整個字符串用雙引號括起來,並且您正在轉義單引號 - 這不是必需的。我相信MySQL實際上會收到查詢中的\字符。這會導致SQL錯誤。除了2. 此外,即使您似乎在其中存儲int,「conf」確實是varchar字段而不是int: – JAL 2010-09-18 15:46:46

+0

@亞歷JL - 更新,但仍然有相同的錯誤!並且'conf'是一個int not varchar – 2010-09-18 15:58:13

+0

如果conf是一個int字段而不是一個varchar - 該值不應該在你的查詢中引用。 – JAL 2010-09-18 18:10:04

回答

1
$result=mysql_query($sql); 

$result=mysql_query($sql) or trigger_error(mysql_error().$sql); 

並重新運行

然後

$email_to=$_POST['email_to']; 

$email_to=mysql_real_escape_string($_POST['email_to']); 

哦對了,還有也引述

+0

注意:您的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第1行的'\'2 \'WHERE'members'.'email' ='[email protected]'處使用正確的語法'UPDATE'cosmos'.'members' SET'conf' = \'2 \'WHERE'members'.'email' ='[email protected]'; – 2010-09-18 15:37:46

+1

@tunetosuraj似乎你以某種方式被添加到「大約2」。它應該只是conf ='2' – 2010-09-18 15:42:32

+0

上面的一個是固定的,但不是'警告:mysql_affected_rows()期望參數1是資源,在C:\ wamp \ www \ a \ l \ forget.php中給出布爾值 – 2010-09-18 15:44:16

0

您執行的SQL不是SELECT,因此沒有行被返回!

+1

+1。相反,OP需要的是[mysql_affected_rows](http://www.php.net/manual/en/function.mysql-affected-rows.php)。 – casablanca 2010-09-18 15:34:10

+2

正確。但它不是唯一的問題,它的stil布爾值,而不是資源 – 2010-09-18 15:35:24

+0

確實,謝謝你的提示。 – fredley 2010-09-18 15:35:29

2

$resultfalse因爲您的查詢無效(有語法錯誤)。用途:

$sql = "UPDATE members SET conf=2 WHERE email = '$email_to';" 

(注意周圍$email_to引號)

而且mysql_num_rows()應該只用於SELECT查詢。對於UPDATEINSERTDELETE,請改爲使用mysql_affected_rows()。最後,爲了將來的參考,如果你的查詢不起作用,打印錯誤和使用的SQL查詢(比如Col Shrapnel的答案)。它會幫助你知道什麼是錯的。

+0

嘿,sql部分工作,但不是'mysql_affected_rows()' – 2010-09-18 15:41:30

+0

@tune什麼不行? – NullUserException 2010-09-18 15:42:44

+0

仍然得到'警告:mysql_affected_rows()期望參數1是資源,在C:\ wamp \ www \ a \ l \ forget.php'中給出布爾值,重點在於'mysql_affected_rows()' – 2010-09-18 15:45:19

相關問題