Jump to content

PDO equivalent of mysql_num_rows() ?


devxtec

Recommended Posts

I am trying to figure out how I can get the number of rows returned by a fetch. Is there an equivalent of mysql_num_rows() that comes with PDO? I noticed PDOStatement->rowCount(), however; on select statements this is not a guaranteed way of getting back the number of rows returned by the statement as this behavior is not guaranteed for all databases.

 

Anyone have any ideas on how to go about getting the number of rows?

Link to comment
https://forums.phpfreaks.com/topic/142995-pdo-equivalent-of-mysql_num_rows/
Share on other sites

PDO::query("SELECT COUNT(*)"... then the query of what you want to count );
PDOStatement::fetchColumn();

 

that's the proper way using PDO for all database types...

 

What I am trying to tell you, is that you should do the SELECT COUNT(*) first and if PDOStatement::fetchColumn(); returns greater than > 0, then you would do the actually query and loop those results. That's the proper way of doing database SELECT(s). Sure it not the mysql/php way, but it is the way one should always do it when using a DB abstracting layer...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.