简体   繁体   English

MySQL查询结果不是资源

[英]MySQL query results are not a resource

I am having trouble with the query in this code. 我在此代码中的查询遇到麻烦。 The problem is one I have had before, the $num = mysql_num_rows($result); 问题是我以前有过, $num = mysql_num_rows($result); part gives me a MySQL error saying it expected a resource. 部分给了我一个MySQL错误,说它期望资源。 Usually when I have this error it is because I misplaced a single quote some where, but after looking I cannot find any problem, though this query is a bit more complex than what I usually have to deal with. 通常,当我遇到此错误时,是因为我在某个地方放了一个单引号,但是在查找后我找不到任何问题,尽管此查询比我通常要处理的要复杂一些。

//connect to the database and stuff

$last_year = idate("Y")-1;
$month = date("m");
$day = date("d");                                   

$query = "SELECT bills.b_id, bills.c_id, bills.grand_total, bills.void, bills.date_added,
                 customers.b_name, customers.l_name, customers.f_name, customers.phone 
          FROM bills, customers 
          WHERE bills.c_id = customers.c_id 
                AND bills.void = '0' 
                AND date_added BETWEEN '".$last_year."-".$month."-".$day."' AND CURDATE()";
$result = mysql_query($query);
mysql_close($link);                 
$num = mysql_num_rows($result);

EDIT: 编辑:

Although I already know the mysql_close() function is not the problem I went ahead and removed it and my code still does not work. 尽管我已经知道mysql_close()函数不是问题,但我继续将其删除,但我的代码仍然无法正常工作。 This EXACT same code (other than the query) works in nearly a dozen other pages. 完全相同的代码(除查询等)的作品在其他近十几页。 The problem is in the query , the MySQL error (as stated before) is mysql_num_rows() expects parameter 1 to be resource . 问题出在查询中 ,MySQL错误(如前所述)是mysql_num_rows() expects parameter 1 to be resource I am working on getting the specific error now. 我正在努力获取特定的错误。

Add some error handling to your code. 在代码中添加一些错误处理。

$result = mysql_query($query);
if ( !$result ) {
  echo 'the query failed: ', mysql_error();
  die;
}

(in "real" production code you might not want to display the actual query and error message to just any arbitrary user though). (尽管在“真实”生产代码中,您可能不希望向任何任意用户显示实际的查询和错误消息)。

see also: http://docs.php.net/mysql_error 另请参见: http : //docs.php.net/mysql_error

  1. Check to see if there were mysql errors. 检查是否有mysql错误。 If you don't already have error reporting turned on, turn it on for development ( error_reporting(E_ALL); ). 如果尚未打开错误报告,则将其打开以进行开发( error_reporting(E_ALL); )。

  2. Try waiting to close your mysql connection until after you're done with the result sets. 尝试等待关闭mysql连接,直到完成结果集为止。

Try with the mysql_close() function at the end. 最后尝试使用mysql_close()函数。 If you close the mysql connection, mysql_num_rows() is not going to work 如果关闭mysql连接,则mysql_num_rows()无法正常工作

$num = mysql_num_rows($result);
.
.
//Any others mysql operations
.
.
mysql_close($link);

You close link to mysql before retrieving query results. 在检索查询结果之前,请关闭与mysql的链接。 That's the problem. 那就是问题所在。 Just don't use mysql_close() as PHP can automatically handle it. 只是不要使用mysql_close(),因为PHP可以自动处理它。

"not a resource" error means your query failed. “非资源”错误表示您的查询失败。
change your mysql_query call in that manner 以这种方式更改您的mysql_query调用

$result = mysql_query($query) or trigger_error(mysql_error().$query);

and see what's wrong with your query. 并查看您的查询出了什么问题。
always do it this way to keep in touch with every error may occur 始终以这种方式进行操作以与可能发生的每个错误保持联系

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

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