php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #56323 rowCount() fails
Submitted: 2005-02-23 17:12 UTC Modified: 2005-02-27 01:47 UTC
From: lists at cyberlot dot net Assigned:
Status: Not a bug Package: PDO_MYSQL (PECL)
PHP Version: 5.0.3 OS: Fedora Core 3
Private report: No CVE-ID: None
 [2005-02-23 17:12 UTC] lists at cyberlot dot net
Description:
------------
rowCount() returns 0 when it should return a value

While I realize rowCount use on a select is not portable, there has always been the ability to use num_rows within mysql php api's.

Docs suggest this should work with mysql

Same code using normal mysql api works

Reproduce code:
---------------
$dsn = 'mysql:dbname=mysql;host=localhost';
$db1 = new PDO($dsn, 'root', '');
$dsn = 'mysql:dbname=venue;host=localhost';
$db2 = new PDO($dsn, 'root', '');
                                                                                                                                                             
$result2 = $db2->query('SELECT * FROM songs');
echo $result2->rowCount()."\n";
$result1 = $db1->query('SELECT * FROM user');
echo $result1->rowCount()."\n";


Expected result:
----------------
3
5

Actual result:
--------------
0
0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-23 17:43 UTC] lists at cyberlot dot net
I found the reason this doesn't work.. The only way to get the number of rows when doing a select is by calling mysql_store_result within the select statement prior to calling mysql_affected_rows.
 [2005-02-26 19:36 UTC] wez@php.net
You should either "SELECT count(*) ..." or use fetchAll() .
The only way that rowCount() will work, for pretty much all databases, is if you read all the rows up front.

PDO is designed to deliberately not do this by default, with the unfortunate, but not terminal, consequence of having rowCount() do nothing useful most of the time.  M$'s ADO has the same "problem".
 [2005-02-27 01:47 UTC] lists at cyberlot dot net
If this is the expected behaviour then the docs need to be updated.

"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."

This statement should be removed all together.

I also thought that pdo was supposed to be a standard method of access and was not really considered a "database abstraction" layer, but a method of access that a program could be carefull and make portable but portablility was not the goal

I refer to
http://www.oracle.com/technology/pub/articles/php_experts/otn_pdo_oracle5.html

Where you state "Unify the common features of the various RDBMS libraries, but don't exclude the more advanced features."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 05:01:29 2024 UTC