PDO “Uncaught exception 'PDOException' .. Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll().”

前端 未结 3 385
萌比男神i
萌比男神i 2021-01-17 17:56

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:

相关标签:
3条回答
  • 2021-01-17 18:39

    This also happen if you are trying to fetch a non SELECT query (Eg - UPDATE/INSERT/ALTER/CREATE). Make sure to use fetch or fetchAll only for SELECT queries.

    Possible Duplicate Answer/Question

    0 讨论(0)
  • 2021-01-17 18:42

    After struggling with this issue for days, I finally found that this worked for me:

    $db = new PDO ($cnstring, $user, $pwd);
    $db->setAttribute (PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
    
    0 讨论(0)
  • 2021-01-17 18:46

    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)
提交回复
热议问题