错误:警告:mysql_num_rows() 期望参数 1 为资源,布尔值在 C:\xampp\htdocs\...\....php 第 19 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6045476/
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
ERROR: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\...\....php on line 19
提问by iamanapprentice
I keep on receiving an error message using mysql_num_rows()
, can you help me figure out what went wrong on my code?
我使用 不断收到错误消息mysql_num_rows()
,你能帮我找出我的代码出了什么问题吗?
Here's my code:
这是我的代码:
<?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();
}
}
?>
I've tried mysql_errno()
as you advised.. and it echo 1146
我已经mysql_errno()
按照你的建议试过了..它回响了 1146
and when I search for that error it says that
当我搜索该错误时,它说
1146: Table 'kossu.nonexistenttable' doesn't exist
But I don't know what it means... Please help.
但我不知道这是什么意思...请帮忙。
回答by KingCrunch
Probably mysql_query()
failed. In this case it returns false
. Use mysql_error()to find out, what happens.
可能mysql_query()
失败了。在这种情况下,它返回false
. 使用mysql_error()找出会发生什么。
回答by Sander Marechal
When there is an error, mysql_query()
returnse false
instead of a resource object. Check the return value of mysql_query()
before you pass it to mysql_num_rows()
.
出现错误时,mysql_query()
返回sefalse
而不是资源对象。mysql_query()
在将其传递给 之前检查 的返回值mysql_num_rows()
。
回答by Ignacio Vazquez-Abrams
From the docs:
从文档:
Return Values
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
返回值
对于 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回结果集的语句,mysql_query() 在成功时返回资源,在错误时返回 FALSE。
So, you have an error. Use mysql_error()
to find out what.
所以,你有一个错误。使用mysql_error()
找出。
回答by Arihant Nahata
mysql_query()
failed. It returns false
.To see the error use mysql_error()
mysql_query()
失败的。它返回false
.To 查看错误使用mysql_error()
回答by whitehand
It's possible you've not selected the right database. I encountered such a problem before and the error was the database I selected.
您可能没有选择正确的数据库。之前遇到过这样的问题,错误的是我选择的数据库。
回答by Ramesh Bhusal
When the MySQL query is executed it is expected to return the query result. It returns False
if the query was not successfully executed. It seems that your query failed so it returned False
. When you try to count rows with mysql_num_rows()
to the failed query, it will definitely return error. There might be error in your query. Please check.
当执行 MySQL 查询时,预计会返回查询结果。False
如果查询未成功执行,则返回。您的查询似乎失败,因此返回False
. 当您尝试对mysql_num_rows()
失败的查询计算行数时,它肯定会返回错误。您的查询中可能有错误。请检查。
回答by Kalkidan Chekol
The cause could be that the SQL query is using an incorrect database. Use the fully-qualified table name, i.e. databaseName.tableName
.
原因可能是 SQL 查询使用了不正确的数据库。使用完全限定的表名,即databaseName.tableName
.
In your case:
在你的情况下:
$sql = mysql_query("SELECT id FROM DBName.members WHERE username='$customer' AND password='$password' LIMIT 1");
where 'DBName' is your database name.
其中 'DBName' 是您的数据库名称。