[英]Mysql empty query in php - what do I get? Resource ID?
I want to check whether my query is empty (has no rows). 我想检查查询是否为空(无行)。 What is the best way of hecking it? 弄乱它的最好方法是什么?
And what do I get in the following "$result" and "$row" (this is how I am aiming to solve this - if($result)
or maybe using if($row)
means that it is not empty. Any better suggestions? 在下面的“ $ result”和“ $ row”中我能得到什么(这就是我要解决的方式-if if($result)
或使用if($row)
表示它不为空。更好建议?
Thanks. 谢谢。
$result = mysql_query("SELECT * FROM table1 WHERE column1= '$var1'
AND column2 IN ('no','nein','nope')
AND timestamp BETWEEN '$date' AND NOW()");
$row = mysql_fetch_assoc($result);
You can use mysql_num_rows($result)
get the number of rows, or you could see if $row === false
; 您可以使用mysql_num_rows($result)
获取行数,或者可以查看$row === false
;
$row
will be the first row of the recordset OR false
if the query has no results. $row
将成为记录集的第一行;如果查询没有结果,则为false
。 $result
will be a recordset resource if the query succeeded of false
if it had an error. 如果查询成功,如果错误返回false
则$result
将是记录集资源。
If you are this early in the project and/or just learning please consider switching to the new DB interface PDO
. 如果您在项目初期还只是学习和/或只是学习,请考虑切换到新的DB接口PDO
。 At the least use Mysqli
... the mysql extension youre using now is a bit outdated. 至少使用Mysqli
...现在使用的mysql扩展有点过时了。 It works, and sometimes its even required in legacy apps/sites, but it should be avoided if possible. 它可以正常运行,有时甚至在旧版应用程序/站点中也需要,但应尽可能避免。
Test this. 测试一下。
$result = mysql_query("SELECT * FROM table1 WHERE column1= '$var1'
AND column2 IN ('no','nein','nope')
AND timestamp BETWEEN '$date' AND NOW()");
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0) {
echo $row['column name'];
}
Use mysql_num_rows
to get the row count. 使用mysql_num_rows
获取行数。
$result will always be of type Resource if your query didn't fail. 如果您的查询没有失败,则$ result始终为Resource类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.