Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO rowCount not returning correct number of affected rows

Tags:

php

pdo

I'm having a problem with PDO prepared statement and rowCount returning incorrect number of affected rows.

I have a simple test database of:

create table test (
   boolean var1;
);

Then I have the following test code:

$sth = $pdo->prepare("INSERT into test (var1) VALUES (:val)");
$sth->execute(array(':val' => true));
echo $sth->rowCount();

Which returns as expected: 1 rows affected

And when I insert an invalid type and the Insert fails:

$sth = $pdo->prepare("INSERT into test (var1) VALUES (:val)");
$sth->execute(array(':val' => 20));
echo $sth->rowCount();

Which returns as expected: 0 rows affected

However, When I have multiple inserts -

$sth = $pdo->prepare("INSERT into test (var1) VALUES (:val)");

$sth->execute(array(':val' => true));
echo $sth->rowCount() . ", ";

$sth->execute(array(':val' => 20));
echo $sth->rowCount();

Results in: 1, 1

And if I flip execution order I get: 0, 1

Why is the rowCount() -- Affected rows not being set to zero on a fail statement after a success statement?

I'm running php 5.3.6-13 and Postgresql 9.1

like image 311
James Cowhen Avatar asked Mar 01 '12 01:03

James Cowhen


People also ask

Which PDO function would you use to count number of rows returned from a table?

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

Which function counts the number of affected rows from MySQL?

The affected_rows / mysqli_affected_rows() function returns the number of affected rows in the previous SELECT, INSERT, UPDATE, REPLACE, or DELETE query.

How can I count the number of rows in Mysqli using PHP?

We can get the total number of rows in a table by using the MySQL mysqli_num_rows() function. Syntax: mysqli_num_rows( result ); The result is to specify the result set identifier returned by mysqli_query() function.


1 Answers

It seems to me that $sth->execute(array(':val' => true)) completes successfully thus increasing the rowCount, but $sth->execute(array(':val' => 20)) does not. Here is the state of the rowCount for the $sth at each stage:

$sth = $pdo->prepare("INSERT into test (var1) VALUES (:val)");  

# No successful DML queries have been done with the $sth yet.
# rowCount == 0

$sth->execute(array(':val' => true));
echo $sth->rowCount() . ", ";

# rowCount increases because of a successful INSERT statement
# rowCount == 1

$sth->execute(array(':val' => 20));
echo $sth->rowCount();

# rowCount does not increase due to failed INSERT statement
# rowCount == 1

Now, lets look at that in the opposite order:

$sth = $pdo->prepare("INSERT into test (var1) VALUES (:val)");  

# No successful DML queries have been done with the $sth yet.
# rowCount == 0

$sth->execute(array(':val' => 20));
echo $sth->rowCount();

# rowCount does not increase due to failed INSERT statement
# rowCount == 0

$sth->execute(array(':val' => true));
echo $sth->rowCount() . ", ";

# rowCount increases because of a successful INSERT statement
# rowCount == 1
like image 180
dotancohen Avatar answered Nov 10 '22 01:11

dotancohen