I ran in to this problem in one of my live web apps. It seems that if you issue a multi-statement query to MySQL via PHP PDO, and the first statement is an insert statement, and the second statement is an update statement, the PDO::nextRowset() function doesn't return the right number of result sets. (Note that PDO supposedly supports multiple statements per MySQL query since PHP 5.3.)
Here's an example:
SQL:
create database `test`character set utf8 collate utf8_general_ci;
create table `test`.`testtable`( `id` int );
PHP:
<?php
$link = new \PDO('mysql:host=localhost;dbname=test', 'username', 'password');
//Run one of the 4 $handle assignments at a time (comment out all but one).
//Run #4 on an empty table to compare the results of #1 and #4.
//WORKS: INSERT, followed by SELECT, followed UPDATE
//Output:
//Rowset 1
//Rowset 2
//Results detected
$handle = $link->prepare(' insert into testtable(id) values(1);
select * from testtable where id = ?;
update testtable set id = 2 where id = ?;');
//WORKS: SELECT, followed by UPDATE
//Output:
//Rowset 1
//Results detected
$handle = $link->prepare('select * from testtable where id = ?;
update testtable set id = 2 where id = ?;');
//WORKS: UPDATE, followed by SELECT
//Output:
//Rowset 1
//Rowset 2
//Results detected
$handle = $link->prepare('select * from testtable where id = ?;
update testtable set id = 2 where id = ?;');
//DOESN'T WORK: INSERT, followed by UPDATE, followed by SELECT
//Output:
//Rowset 1
//Expected output: same as examples 1 and 3
$handle = $link->prepare('insert into testtable(id) values(1);
update testtable set id = 2 where id = ?;
select * from testtable where id = ?;');
$handle->bindValue('1', '1');
$handle->bindValue('2', '2');
$handle->execute();
$i = 1;
do{
print('Rowset ' . $i++ . "\n");
if($handle->columnCount() > 0)
print("Results detected\n");
}while($handle->nextRowset());
?>
Does anyone have any idea as to what I'm doing wrong? Why can't I put my select statement at the end?
PHP 5.3.5
MySQL 5.1.54
I submitted a PHP bug report about this problem and a possible patch has been submitted. So it looks like this was a PHP bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With