简体   繁体   English

错误日志中的mysql_num_rows

[英]mysql_num_rows in the error log

I am encountering an error. 我遇到一个错误。 My log is as follows: 我的日志如下:

PHP Warning:  mysql_num_rows(): supplied argument is not 
a valid MySQL result resource in     
/home/domain/public_html/index.php on line 96

My code is as followsL line 96: 我的代码如下,第96行:

mysql_num_rows(mysql_query("SELECT * FROM `table` WHERE 
UNIX_TIMESTAMP(`date`)>$user_last_visit"));

What could cause this error? 是什么导致此错误?

I would try rearranging the code 我会尝试重新排列代码

$sql = "SELECT * FROM `table` WHERE UNIX_TIMESTAMP(`date`) > $user_last_visit";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);

I can't tell from your code but you are creating a connection to the mysql server? 我无法从您的代码中看出来,但是您正在创建与mysql服务器的连接?

If you only want to count the rows, use the COUNT(*) keyword in the query; 如果只想计算行数,请在查询中使用COUNT(*)关键字; otherwise, the data base will prepare the whole resulting rows for output although you don't need them. 否则,数据库将准备整个结果行以供输出,尽管您不需要它们。

SELECT COUNT(*)
FROM `table`
WHERE UNIX_TIMESTAMP(`date`) > $user_last_visit

Then, simple execute the query: 然后,简单执行查询:

$result = mysql_query($query);
list($count) = mysql_fetch_array($result);

Anyway, your query throws an error; 无论如何,您的查询会引发错误; there should be another warning showing the error. 应该有另一个警告显示错误。 You can use mysql_error() to find out the error otherwise. 否则,您可以使用mysql_error()找出错误。

According to http://php.net/manual/en/function.mysql-query.php mysql_query returns "For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error." 根据http://php.net/manual/en/function.mysql-query.php,mysql_query返回“对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回结果集的语句,mysql_query()成功返回资源,否则返回FALSE错误。”

So try doing 所以尝试做

$query = "SELECT * FROM `table` WHERE UNIX_TIMESTAMP(`date`)>$user_last_visit"
$resource = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
mysql_num_rows($resource);

And then see what happens. 然后看看会发生什么。

You put two functions into each other directly: 您将两个函数直接相互放置:

mysql_num_rows(mysql_query(...));

Technically this is possible to do (function 1 returns something that becomes the parameter of function 2), however, in case function 1 returns something that function 2 can not deal with, you get errors. 从技术上讲,这是可以做到的(函数1返回的内容成为函数2的参数),但是,如果函数1返回了函数2无法处理的内容,则会出错。

That happens in your case. 在您的情况下会发生这种情况。 Instead store the return value into a variable and do error handling your own: 而是将返回值存储到变量中,并自行处理错误:

$result = mysql_query(...);
if (!$result) {
    throw new Exception(sprintf('Database query failed: %s', mysql_error()));
}
$num_rows = mysql_num_rows($result);

Proper error handling is crucial for solid programming, so take care about return values. 正确的错误处理对于可靠的编程至关重要,因此请注意返回值。 When in doubt double-check for the function in question with the PHP manual which is really a great resource. 如果有疑问,请使用PHP手册仔细检查有问题的功能,这确实是一个很好的资源。 See mysql_query . 参见mysql_query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM