问题描述
我知道这个问题已经被问了很多次,但是我已经阅读了许多问题的答案,但仍然不明白为什么我收到这个错误:
I know this question has been asked many times, but I've read the answers to many of the questions and still cannot understand why I am receiving this error:
第一件奇怪的是,我没有在本地主机(wampserver)上收到错误,但是我在网络服务器上找到它。我的本地主机上的php版本是5.3.10,在我的网络服务器上是5.3.13。
The first thing that is odd, is that I do not get an error on my localhost (wampserver), but I do get it on my web server. The php version on my localhost is 5.3.10, and on my web server it is 5.3.13.
我已经看到这个错误的根源在进行查询当数据从先前的查询中留在缓冲区中时。这不是我的情况 - 我已经回显出所有的数据,我知道一个事实,每个行返回在一个查询被抓取。
I have read that the source of this error is making a query when data left in the buffer from a previous query. This is not the case for me -- I have echo'd out all of the data and I know for a fact that every row returned in a query is being fetched.
就是说,我发现将我的一个查询更改为 fetchAll
而不是 fetch
修复了这个问题,但是它根本就没有,因为我知道所有返回的行都被读取。当我使用 fetchAll
查询(正在循环中)时,我打印出每个循环的数组,每个查询中只有一个项目在数组中循环。
With that said, I have found that changing one of my queries to fetchAll
instead of fetch
fixes the problem, but it simply makes no since because I know that all of the rows returned are being read. When I used fetchAll
for the query (it is being made in a loop), I printed out the array each loop, and only one item was in the array for each query in the loop.
还有一条信息。这不是我更改为 fetchAll
(导致错误消失)的抛出PDO错误的查询,稍后我的php文件中有另一个查询会引发错误。我的文件基本是这样的:
One more piece of information. It's not the query that I changed to fetchAll
(which makes the error go away) that throws the PDO error, there is another query later in my php file that throws the error. My file is basically like this:
... code ...
query 1
... code ...
loop
query 2
end loop
... code ...
query 3
如果我注释掉查询3,没有错误。如果我注释掉,或更改为 fetchAll
,查询2,没有错误。查询1没有任何影响。
If I comment out query 3, there is no error. If I comment out, or change to fetchAll
, query 2, there is no error. query 1 has no affect whatsoever.
我还想补充说,我已经尝试将 LIMIT 1
添加到所有页面上的查询(同时),错误仍然存在。我认为这证明缓冲区中没有未读的数据,对吧?
I would also like to add that I have tried adding LIMIT 1
to all of the queries on the page (at the same time), and the error is still there. I think this proves there is not unread data in the buffer, right?
我真的很困惑,所以我很感激你的建议。在有人问之前,我无法发布完整的代码,但这里是我的代码的简化版本:
I'm really confused, so I would appreciate your advice. Before someone asks, I can't post the full code for this, but here is a simplified version of my code:
$stmt = $this->db->prepare('SELECT ... :par LIMIT 1');
makeQuery($stmt, array(':par' => $var));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt = $this->db->prepare('SELECT ... :par LIMIT 1');
for loop
makeQuery($stmt, array(':par' => $var));
$row2 = $stmt->fetch(PDO::FETCH_ASSOC);
... [use row2] ...
end for loop
$stmt = $this->db->prepare('SELECT ... :par LIMIT 1');
makeQuery($stmt, array(':par' => $var));
$row3 = $stmt->fetch(PDO::FETCH_ASSOC);
这里是 makeQuery()
p>
Here is makeQuery()
.
/**************************************************************************************************************
* Function: makeQuery *
* Desc: Makes a PDO query. *
* Pre conditions: The statement/query and an array of named parameters (may be empty) must be passed. *
* Post conditions: The PDO query is executed. Exceptions are caught, displayed, and page execution stopped. *
**************************************************************************************************************/
function makeQuery($stmt, $array, $errMsg = '')
{
try
{
$stmt->execute($array);
}
catch (PDOException $e)
{
print $errMsg != ''?$errMsg:"Error!: " . $e->getMessage() . "<br/>";
die();
}
}
感谢您的帮助!
编辑:我也尝试在查询2之后执行以下操作(因为这似乎是问题的根源:
I also tried doing the following after query 2 (since that seems to be the source of the problem:
$row2 = $stmt->fetch(PDO::FETCH_ASSOC); var_dump($row2);
输出为:
bool(false)
我偶然遇到了一个PDO错误?
Have I stumbled across a PDO bug?
推荐答案
你需要抓取,直到行提取尝试失败,我知道你可能只有一行在结果集中,认为一个提取是足够的,但不是。
you need to fetch until a row fetch attempt fails. I know you may only have one row in the result set and think one fetch is enough, but its not.
您可能还有其他语句,您没有完全抓取,直到获取失败。是的,我看到您提取,直到提取失败为一个的语句。
you probably have other statements where you didn't fully "fetch until a fetch failed". Yes, i see that you fetch until the fetch failed for one of the statements.
另请参见closecursor()
also, see closecursor()
$sql = "select * from test.a limit 1";
$stmt = $dbh->prepare($sql);
$stmt->execute(array());
$row = $stmt->fetch();
$stmt->closeCursor();
或
$sql = "select * from test.a limit 1";
$stmt = $dbh->prepare($sql);
$stmt->execute(array());
list($row) = $stmt->fetchAll(); //tricky
这篇关于PDO“未捕获异常”PDOException'..其他无缓冲查询处于活动状态时,无法执行查询。考虑使用PDOStatement :: fetchAll()。“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!