PHP (MySQL) 错误:“警告:mysql_num_rows() 期望参数 1 为资源”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3742239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP (MySQL) error : "Warning: mysql_num_rows() expects parameter 1 to be resource"
提问by Suraj
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 <[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";
}
}
Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\a\l\forget.php on line 20
警告:mysql_affected_rows() 期望参数 1 是资源,布尔值在 C:\wamp\www\a\l\forget.php 第 20 行给出
回答by NullUserException
$result
is false
because your query is invalid (has a syntax error). Use:
$result
是false
因为您的查询无效(有语法错误)。用:
$sql = "UPDATE members SET conf=2 WHERE email = '$email_to';"
(note the quotes surrounding $email_to
)
(注意周围的引号$email_to
)
Also mysql_num_rows()
should be used for SELECT
queries only. For UPDATE
, INSERT
and DELETE
, use mysql_affected_rows()
instead.
也mysql_num_rows()
应该仅用于SELECT
查询。对于UPDATE
,INSERT
和DELETE
,请mysql_affected_rows()
改用。
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.
最后,为了将来参考,如果您的查询不起作用,请打印错误和使用的 SQL 查询(类似于 Col Shrapnel 的回答中的内容)。它将帮助您了解问题所在。
回答by Your Common Sense
$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
哦,是的,还有引号
回答by fredley
The SQL you performed was not a SELECT, so no rows are returned!
您执行的 SQL 不是 SELECT,因此没有返回任何行!