PHP(MySQL)错误:“警告:mysql_num_rows()期望参数1是资源”

时间:2010-09-18 15:30:22

标签: php mysql

  

可能重复:
  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 <cosmos@gmail.com>"; 
    $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为资源,布尔值在第20行的C:\ wamp \ www \ a \ l \ forget.php中给出

3 个答案:

答案 0 :(得分: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的回答)。它会帮助你知道什么是错的。

答案 1 :(得分: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']);

哦,是的,还有引号

答案 2 :(得分:0)

您执行的SQL不是SELECT,因此不返回任何行!