PDO and mysql_num_rows

Is it just me or is it a bit strange that they forgot to include a method similar to mysql_num_rows in the PDO class? Now you have to execute two queries just to find out how many rows are found in a SELECT query. Seems a bit silly to me! :frowning: Lets hope there’s an update in the pipeline!

sorry, i’m newcomer.

what is PDO…and can u show me example of ‘two queries’ you said.


Trims.

I’ve not read an official explanation. it might be to do with the underlying library that PDO interacts with (if that’s difrerent from the old mysql functions). Or it might be that, since PDO is intended to be database access layer for a number of different databases, only those functions common to or equivalent in several different supported RDBMS have been implemented.

Either way, it’s a minor inconvenience to run another query like

SELECT COUNT(id) FROM mytable WHERE foo='bar'

Quite often, the number of results exceeds the number one wishes to display. If you want to paginate using a LIMIT clause, you’ll have to run that query to get the total anyway.

Look here.

In order to find out how many rows were fond with a SELECT statement, you first need to count the rows…


$sql = "SELECT count(*) FROM `table` WHERE foo = bar";
$result = $con->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn()	

That just retrieves the number of rows. To get the actual data you then have to do the same query but replace count(*) with the columns you require.

Whereas in the old mysql world, you could do both with one statement…


$sql = "SELECT * FROM `table` WHERE foo = bar";
$result = mysql_query($sql);
$number_of_rows = mysql_num_rows($result);

@auricle
You are right. It is only a minor inconvenience, and maybe I should just stop moaning! :smiley: