简体   繁体   中英

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. 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. Executing that via the database class returns false.

Has anyone seen this behavior? No mysql_errorno or error messages are coming back.

$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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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