PHP (MySQL) error : “Warning: mysql_num_rows() expects parameter 1 to be resource” [duplicate]

匿名 (未验证) 提交于 2019-12-03 07:36:14

问题:

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

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 ";      $messages= "Hey $name ,\r\n";     $messages.="You recently requested a new password";     $messages.="
Confirmation Link \r\n"; $sentmail = mail($to,$subject,$messages,$header); echo $messages; } else { echo "Not found your email in our database"; } }

Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\a\l\forget.php on line 20

回答1:

$result=mysql_query($sql); 

to

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

and run it again

and then

$email_to=$_POST['email_to']; 

to

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

oh yes, and there is also quotes



回答2:

$result is false because your query is invalid (has a syntax error). Use:

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

(note the quotes surrounding $email_to)

Also mysql_num_rows() should be used for SELECT queries only. For UPDATE, INSERT and DELETE, use mysql_affected_rows() instead.

Finally, for future reference, if your query doesn't work, print the error and the SQL query used (something like what's on Col Shrapnel's answer). It will help you know what's wrong.



回答3:

The SQL you performed was not a SELECT, so no rows are returned!



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!