错误:警告:mysql_num_rows() 期望参数 1 是资源,布尔值在 C:xampphtdocs.


ERROR: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:xampphtdocs.......php on line 19

我不断收到mysql_num_rows()错误消息,你能帮我找出我的代码出了什么问题吗?

这是我的代码:

<?php 
//check if the user press submit
if (isset($_POST['submit'] )) {
    $customer = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]); // filter everything but numbers and letters
    $password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]); // filter everything but numbers and letters
    $sql = mysql_query("SELECT id FROM members WHERE username='$customer' AND password='$password' LIMIT 1"); // query the person
    // ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
    $existCount = mysql_num_rows($sql); // count the row nums
    if ($existCount == 1) { // evaluate the count
         while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
         }
         $_SESSION["id"] = $id;
         $_SESSION["customer"] = $customer;
         $_SESSION["password"] = $password;
         header("location: index.php");
         exit();
    } else {
        echo 'That information is incorrect, try again <a href="customer_login.php">Click Here</a>';
        exit();
    }
}
?>

我已经按照你的建议尝试了mysql_errno()..它回声1146

当我搜索该错误时,它说

1146: Table 'kossu.nonexistenttable' doesn't exist

但我不知道这是什么意思...请帮忙。

可能mysql_query()失败了。在这种情况下,它返回false .使用 mysql_error() 找出会发生什么。

当出现错误时,mysql_query()返回false而不是资源对象。在将 mysql_query() 传递给 mysql_num_rows() 之前,请检查它的返回值。

从文档中:

返回值

对于返回结果集的 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他语句,mysql_query() 在成功时返回资源,在错误时返回 FALSE。

所以,你有一个错误。使用mysql_error()找出内容。

mysql_query()失败了。它返回false .要查看错误,请使用mysql_error()

您可能没有选择正确的数据库。我以前遇到过这样的问题,错误是我选择的数据库。

当执行MySQL查询时,它应该返回查询结果。如果查询未成功执行,它将返回False。您的查询似乎失败了,因此它返回了False .当您尝试对失败查询mysql_num_rows()行进行计数时,它肯定会返回错误。查询中可能存在错误。请检查。

原因可能是 SQL 查询使用了不正确的数据库。 使用完全限定的表名,即 databaseName.tableName .

在您的情况下:

$sql = mysql_query("SELECT id FROM DBName.members WHERE username='$customer' AND password='$password' LIMIT 1"); 

其中"DBName"是您的数据库名称。

相关文章: