Friday, 29 July 2011

PDO mysql_num_rows equivalent

So I'm re-doing a web app using PDO rather than the built-in MySQL stuff as there seems to be a possibility that the MySQL extension will be gone before too long.

Thing is I do a lot, and I mean a hell of a lot, of checking to see how many rows are returned after a SELECT... and PDO doesn't support mysql_num_rows.

So after looking around I discovered that if I alter my SQL I can check on one line thus:

$dbconn->query("SELECT COUNT(*) FROM `something` WHERE `condition` = 'something'")->fetchColumn()

Which'll give me the number of matching records. I can then check that against a condition and carry on... or not as the case may be.