mysql_unbuffered_query pdo,PDO“未捕获异常”PDOException'..其他无缓冲查询处于活动状态时,无法执行查询。考虑使用PDOStatement :: fetchA...

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:

Fatal error: Uncaught exception 'PDOException' with message

'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while

other unbuffered queries are active. Consider using

PDOStatement::fetchAll(). Alternatively, if your code is only ever

going to run against mysql, you may enable query buffering by setting

the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.'

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.

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.

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

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.

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

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() . "
";

die();

}

}

Thanks for your help!

EDIT: 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);

The output was:

bool(false)

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.

also, see closecursor()

$sql = "select * from test.a limit 1";

$stmt = $dbh->prepare($sql);

$stmt->execute(array());

$row = $stmt->fetch();

$stmt->closeCursor();

or

$sql = "select * from test.a limit 1";

$stmt = $dbh->prepare($sql);

$stmt->execute(array());

list($row) = $stmt->fetchAll(); //tricky

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值