gpt4 book ai didi

多语句查询中的PHP PDO错误

转载 作者:可可西里 更新时间:2023-11-01 07:06:32 25 4
gpt4 key购买 nike

我在我的一个在线网络应用程序中遇到了这个问题。似乎如果您通过 PHP PDO 向 MySQL 发出多语句查询,并且第一条语句是插入语句,第二条语句是更新语句,则 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());
?>

有人知道我做错了什么吗?为什么我不能将 select 语句放在末尾?

PHP 5.3.5

MySQL 5.1.54

最佳答案

我提交了一个 PHP bug report关于这个问题,并且已经提交了一个可能的补丁。所以看起来这是一个 PHP 错误。

关于多语句查询中的PHP PDO错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9388928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com