mysqli_num_rows函数

时间:2016-09-19 16:37:56

标签: php mysqli row

我被困了......希望得到一些帮助。

我对这种情况有疑问。我一直在按照教程为我的网站添加搜索功能,但我收到了以下错误:

警告:mysqli_num_rows()期望参数1为mysqli_result,第48行的C:\ xampp \ htdocs \ registracija \ index.php中给出布尔值

这是我的代码:

if ($password_form==$repassword_form) {

    $user=mysqli_query(
         $con,
         "SELECT * FROM users,
          WHERE username=username_form
          OR email=email_form
      ");
    $counter=mysqli_num_rows($user);
    if ($counter==0) {

      if (move_uploaded_file($temporary_name, $path)) {
            echo "YES!<br />";
          }       
          else   {
            echo "NO!<br />";
    }       
  } 
  else   {
        echo "ERROR!<br />Some Message!<br />";
   }

 }   
 else   { echo "ERROR!<br />Some Message!<br />";
 }
}
else { 
?>

1 个答案:

答案 0 :(得分:0)

您的查询中有错误,请更改此信息:

"SELECT * FROM users,
      WHERE username=username_form
      OR email=email_form
  "

以删除逗号:

"SELECT * FROM users
      WHERE username=username_form
      OR email=email_form
  "

此外,username_form和email_form似乎可能是字符串,因此您的最终查询可能应如下所示:

"SELECT * FROM users
      WHERE username = '$username_form'
      OR email = '$email_form'
  "

您的查询失败,然后您正尝试执行此操作mysqli_num_rows()

您应该强烈考虑准备好的语句以避免SQL注入:

$stmt = $db->prepare("SELECT * FROM table WHERE username=:un AND email=:em");
$stmt->execute(array(':un' => $username_form, ':em' => $email_form));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

从这篇优秀文章here中了解有关SQL注入的更多信息。