drc15469 2019-03-22 19:04
浏览 108
已采纳

无缓冲的查询错误 - 如何在localhost上获取它们

Sometimes I'm getting this error (on remote server only):

General error: 2014 Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll()...

I added this line on top of my php code - without success:

PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute;

Two questions:

  • How to properly avoid this error, if possible without using fetchAll()?

  • What should I do to get this error on my localhost, if the error exists?

I'm on Windows 7, xampp, phpmyadmin...

here is the code

$st = $db->query("select title from folders where id = " . $_GET['fd'] . " limit 1");
$st->execute();
$title = $st->fetchColumn();
$stb = $db->query("select * from arts where folder = " . $_GET['fd'] . " order by ind asc"); 

error is on the last line

  • 写回答

1条回答 默认 最新

  • doufu5521 2019-03-22 19:34
    关注

    Simple answer

    1. Create a new (additional) connection.

    Once your in the middle of an un-buffered request that connection is locked/block up. Pretty much unusable.

    1. Or don't do it (while the buffered query is on-going).

    So if you need to query other things, either do that before the unbuffered part, or open a second buffered (normal) connection to the DB.

    I once pulled 140 million rows out of the DB with an un-buffered query. Now they are in MongoDB (even though I have mixed feelings about this). PS. if anyone knows how to improve mongo's count performance I am all ears.

    You only really need un-buffered query if your working with a Large dataset, and I mean > 500k rows. I use it as a last resort.

    Anyway, good luck. It's a pain.

    Update

    For you case, you need to make the PDOStatement object go away or call PDOStatement::closeCursor.

    $st = $db->query("select title from folders where id = " . $_GET['fd'] . " limit 1");
    $st->execute();
    $title = $st->fetchColumn();
    unset($st); //<---- unset the statement object
    
    $stb = $db->query("select * from arts where folder = " . $_GET['fd'] . " order by ind asc"); 
    

    If I am thinking about it right, that unset should take care of it. Normally this happens when it goes out of scope such as the end of a method or function and no reference to it. This is sort of like free_result or whatever it was for Mysqli

    I just assumed you had something like this:

    $st = $db->query("select title from folders where id = " . $_GET['fd'] . " limit 1");
    $st->execute();
    foreach($st->fetchColumn() as $col){
         $stb = $db->query("select * from arts where folder = {$col} order by ind asc"); 
    }
    

    Where you were using the results of an open Query for a new query. Which is a no-no. In a case like above the only thing you can do is open a new connection for the part in the loop as it may not be possible to do it all in one Query (for whatever reason).

    In any case change this to proper prepared statements:

    $st = $db->prepare("select title from folders where id = :id limit 1");
    $st->execute(['id' => $_GET['fd']]);
    

    As it is your vulnerable to SQLInjection. It's very easy to fix in PDO, and it's very bad to not do it.

    Cheers!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog