简体   繁体   中英

How to check if table is empty , Execute and FetchRow methods in php and mysql

$RSGetID = $this->MyDBObject->Prepare("SELECT FinalID FROM clothes 
WHERE ClothID=:|1 AND PriceID = :|2 LIMIT 1");

$RSGetID->Execute(2, 199);

$ClothIDRow = $RSGetID->FetchRow();  

return $ClothIDRow->FinalID;

This last line gives an error, because there are no rows in the table, so it says:

"the query did not return any records"

How do I put a condition, that if the table is empty then return 0 , else return the fetched FinalID from the database table?

You're using some custom DB layer (MyDBObject?) rather than straight-up PDO - it's impossible for us to know how this behaves. There's probably a method along the lines of ->RowCount() or ->NumRows() you can call to see if you got anything back after the ->Execute() - but this is just guessing, since I can't see the DB object you're using.

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