多语句查询中的PHP PDO错误

前端之家收集整理的这篇文章主要介绍了多语句查询中的PHP PDO错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在我的一个实时网络应用程序中遇到了这个问题.看来如果你通过PHP PDO向MysqL发出一个多语句查询,并且第一个语句是一个insert语句,而第二个语句是一个update语句,那么PDO :: nextRowset()函数不会返回正确的数字结果集(请注意,自PHP 5.3起,PDO应该支持每个MySQL查询的多个语句.)

这是一个例子:

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());
?>

有没有人知道我做错了什么?为什么我不能把我的选择声明放在最后?

PHP 5.3.5

MysqL 5.1.54

最佳答案
我提交了一个关于这个问题的PHP bug report,并且已经提交了一个可能的补丁.所以看起来这是一个PHP错误.
原文链接:https://www.f2er.com/mysql/433055.html

猜你在找的MySQL相关文章