-2

Can PDO return the number of rows in a db when using a SELECT staement? And does php even support named parameters?

Phil
  • 157,677
  • 23
  • 242
  • 245
stevenmw
  • 689
  • 2
  • 8
  • 15
  • 2
    Yes to both quesions. – ironcito Aug 07 '13 at 04:35
  • Whilst you can generally use [`PDOStatement::rowCount()`](http://php.net/manual/pdostatement.rowcount.php) with MySQL and `SELECT` statements, it is not portable. You are advised to issue a `SELECT COUNT(*)` query with the same predicates to retrieve an accurate row count. – Phil Aug 07 '13 at 04:38
  • But those aren't full proof. Php.net even says row count only works part of the time. http://php.net/manual/en/pdostatement.rowcount.php Wouldn't this be a case to use mysqli\ instead of pdo? – stevenmw Aug 07 '13 at 04:40
  • @user2651294 I don't understand what you're saying / asking. With the MySQL PDO driver, `rowCount()` will work with `SELECT` queries whereas it will not work with say the Postgres driver. Using a separate `COUNT` query will work on just about any driver. And of course, the MySQLi extension *only* works with MySQL – Phil Aug 07 '13 at 04:50
  • Yeah but if youre a web dev do you really need anything other than mysql? Why use pdo over mysqli like it is better when all you're using is mysql anyway. Read this http://php.net/manual/en/pdostatement.rowcount.php with pdo row count does not always work with select staements and if you use COUNT() it is much harder to store the data than with mysqli. – stevenmw Aug 07 '13 at 05:03
  • 1
    @user2651294 You've totally lost me. There are **many** reasons to use a different DBMS (too many to discuss here). As I've already stated, `PDOStatement::rowCount()` **does work** (at the time of writing this comment) when you're using the MySQL driver. It is also trivial to store the value returned from a `COUNT` query. Finally, when using MySQL, the choice of PDO or MySQLi would be one of personal preference. Personally, I find the MySQLi parameter and result binding API overly complicated whereas PDO is much simpler. – Phil Aug 07 '13 at 05:32
  • 1
    Also, as a *web dev* I have personally worked with MySQL, SQLite, PostgreSQL, SQL Server, Oracle and MongoDB in PHP environments. – Phil Aug 07 '13 at 05:58

4 Answers4

1

Can PDO return the number of rows in a db when using a SELECT staement?

There are no limitations in selecting whatever data from db.
PDO can return number of rows in a table as well as any other information you may wish to select from database. Just select it. And you will have it without any problem. That's all.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

Can PDO return the number of rows in a db when using a SELECT staement?

I imagine that confusion stems from this sentence in the manual:

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

http://php.net/manual/en/pdostatement.rowcount.php

It's the database which supports or does not support this feature. When you issue a SELECT * FROM foo ... statement, you're telling the database you'd like to get all the data in the foo table [according to some conditions]. The emphasis here being on data. The database will evaluate all the data it has for that table to give back to you what you asked for. This may or may not be helped by some indices, this may or may not happen asynchronously, the database may or may not know how many results it'll be able to deliver. This all depends on the internals of the database. In some forms of storage, the database simply may not know how many results there will be until it has actually fetched all of them.

Therefore when you call PDOStatement::rowCount, the result may or may not be accurate depending on the database. The PDO API just exposes whatever the database supplies, and since PDO can be used to talk to many different databases, its capabilities depend on what database exactly you're talking to.

And does php even support named parameters?

PHP?! Yes, PHP has many database APIs which support parameterized queries with named parameters. PDO is among them.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    PDO in it's adolescent age had no row count for mysql either, I believe. You can find some relic questions here on SO back in '08 proving that. Though quite useless feature anyway. – Your Common Sense Aug 07 '13 at 08:10
  • 1
    Fair enough, that's apparently a bug that existed sometime between 5.1.6 and 5.2.2 and has been [fixed over half a decade ago](http://php.net/ChangeLog-5.php#5.2.2), so it's hardly a concern anymore. :) – deceze Aug 07 '13 at 08:41
0

The following example shows how to get the rows that was selected or affected in PDO:

<?php
    $selectsql = $database->prepare('SELECT * FROM table');
    $selectsql->execute();
    $count = $selectsql->rowCount();
?>

OR

<?php
    $selectsql = $database->prepare('SELECT COUNT(*) FROM table');
    $selectsql->execute();
?>

With regards to Named Parameters

https://wiki.php.net/rfc/namedparameters

Named Parameters is a topic that has been raised many times over the years. The pros and cons are captured in a variety of emails and (obviously) the feature has not gained support and is not implemented. Since the topic continually gets re-raised, and now PHP has an RFC process, the discussion should be recorded in an RFC (Note this is yet to be done) so the same arguments don't have to be revisited.

I did however, find some way of simulating it:

Simulating PHP Named Parameters

PHP and the lack of Named Parameters

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
  • I think the OP was referring to named placeholders in queries, ie `SELECT ... WHERE foo = :foo`. Could be wrong though – Phil Aug 07 '13 at 04:48
  • If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications. - [PDOStatement::rowCount](http://php.net/manual/en/pdostatement.rowcount.php) – ironcito Aug 07 '13 at 05:01
  • I was. row count does not always work. What is the alternative? If i use COUNT() how can I make a variable with my number of rows stored in it like mysqli? What i am asking is what is there a full proof method to count rows with pdo and select statements and store the count – stevenmw Aug 07 '13 at 05:04
  • 1
    `$query = $db->query('select count(*) from table'); $matches = $query->fetchColumn();` – ironcito Aug 07 '13 at 11:32
0

When using the MySQL driver, these pieces of code are equivalent

// using rowcount
$stmt = $pdo->prepare('SELECT * from `foo` WHERE `bar` = :bar');
$stmt->execute(array('bar' => 'bar'));
$count = $stmt->rowCount();

// using a COUNT query
$stmt = $pdo->prepare('SELECT COUNT(1) from `foo` WHERE `bar` = :bar');
$bar = 'bar';
$stmt->bindParam('bar', $bar);
$stmt->execute();
$count = $stmt->fetchColumn();

I've also illustrated using named placeholders in the query (which is entirely possible).

The first piece of code will most probably not work with another DBMS whereas the second will.

Phil
  • 157,677
  • 23
  • 242
  • 245