php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #56551 PDOStatement::rowCount() giving 0 with ODBC driver even when INSERT succeeds
Submitted: 2005-09-16 11:30 UTC Modified: 2005-09-30 06:57 UTC
From: matthew_peters at uk dot ibm dot com Assigned:
Status: Closed Package: PDO_ODBC (PECL)
PHP Version: 5_1 CVS-2005-09-16 (dev) OS: WinXP
Private report: No CVE-ID: None
 [2005-09-16 11:30 UTC] matthew_peters at uk dot ibm dot com
Description:
------------
I find the following piece of code gives different results depending on whether I use the MySQL driver (I get value 1) or the ODBC driver (I get value 0). In both cases the INSERT has actually succeeded. Am I doing something wrong?

FWIW the levels I am using are a 5.1.0RC2 20050916 version of PHP (I am wedded to this version but presumably that is good since it will contain the most recent version of PDO) but PDO drivers taken from a RC1 build (I can't find any relevant PDO drivers in the RC2 build output on snaps - only a firebird one, and didn't succeed when I tried to build them myself as part of the PHP build). 

Incidentally if I use a literal SQL statement and $rows->$dbh->exec('INSERT...'); then the rows number comes out right both times.

Reproduce code:
---------------
$stmt = 'INSERT INTO company (name) VALUES(?)';
$pdo_stmt = $dbh->prepare($stmt);
$value_list = array('Acme');
$pdo_stmt->execute($value_list);
$rows_affected = $pdo_stmt->rowCount();
echo "rows = $rows_affected\n";

Expected result:
----------------
I want to see a value of 1 for the row I have just inserted. 

Actual result:
--------------
0 when $dbh is pointed at db2 with odbc, 1 when $dbh is pointed at mysql.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-29 10:41 UTC] matthew_peters at uk dot ibm dot com
When I compare the respective statement executors for mysql and odbc:
    (i.e. files ...\pdo_odbc\odbc_stmt.c and ...\pdo_mysql\mysql_statement.c) 
I find that routine pdo_mysql_stmt_execute picks up the row count after executing the query:
    row_count = mysql_stmt_affected_rows(S->stmt);
after executing the query whereas routine odbc_statement_execute does not. 

I don't know if this is the right fix but if in odbc_statement_execute I add a declare:
	long row_count = -1;

and down the bottom, after the switch on the rc from the execute the two lines:
	rc = SQLRowCount(S->stmt, &row_count);
	stmt->row_count = row_count;

then the row count is set correctly.
 [2005-09-30 00:20 UTC] wez@php.net
Thanks for digging deeper into this Matthew; I've committed the change that you suggested.
It will be included in the next pecl release.
 [2005-09-30 06:57 UTC] matthew_peters at uk dot ibm dot com
Wez, thank you.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Dec 03 14:01:27 2020 UTC