简体   繁体   English

带有LIMIT 1的MySQL SELECT *是否没有结果就返回FALSE?

[英]Does MySQL SELECT * with a LIMIT 1 return FALSE on no results?

I've been working on a database wrapper class and every now and then the server goes away... Typical 2006 error message for MySQL. 我一直在研究数据库包装器类,并且服务器不时消失……MySQL的典型2006错误消息。 I've wrapped logic around handling reconnecting to the database and that seems to be working. 我已经在处理重新连接到数据库的过程中包装了逻辑,这似乎正在工作。 What's interesting to me is that this query: 对我而言,有趣的是该查询:

SELECT id FROM pixel WHERE (id = 522574) AND (advertiser_entity_id = 45574) LIMIT 1

Executing that line in PHPMYADMIN yields an empty set. 在PHPMYADMIN中执行该行会产生一个空集。 Executing that via the database class returns false. 通过数据库类执行该操作将返回false。

Has anyone seen this behavior? 有人看到过这种行为吗? No mysql_errorno or error messages are coming back. 没有mysql_errorno或错误消息回来。

$result = mysql_query($query, $this->database_connection); 
if (false === $result) { 
// handle error here 
} 
else { return $result; } 

It isn't an error to not find a matching row. 找不到匹配的行不是错误。 If it doesn't find any records that meet your WHERE conditions, that's not an error, it just returns an empty result. 如果找不到符合您WHERE条件的记录,则不是错误,它只会返回空结果。 If you're writing your own DB layer, it also shouldn't treat it as an error - the query was run just fine, it just didn't find any matches. 如果您正在编写自己的数据库层,则也不应将其视为错误-查询运行得很好,只是找不到任何匹配项。

If you're asking, though, why your code returns false, it's probably just because usually languages treat a zero or null value as "false" in a boolean context. 但是,如果您要询问代码为什么返回false,则可能是因为通常在布尔上下文中语言将零或null值视为“ false”。

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

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