-1

This problem is proceeding to drive me crazy...

First a disclaimer, I have very little formal programming training. I am stumbling thru this.

I am receiving the General error: 2014 error; at first it was one particular query (which was established in a class) and the warning was dependent upon where in the code I was instantiating the object. I proceeded to change every query to a fetchAll and then closed the cursor for every query as well after the fetchAll didn't work. Now there are two offending queries. Here is a copy and paste of one:

(UPDATED CODE):

$sql = "select initial_state from source_nodes where id = :id";

$core = Core::getInstance();
$stmt = $core->dbh->prepare($sql);
$stmt->bindParam(':id', $allSources->id[$id], PDO::PARAM_INT);

if ($stmt->execute()) {
    $row = $stmt->fetchAll();
    $stmt->closeCursor();
    foreach($row as $i=>$value){
        $allSources->state[$id] = $row[$i]['initial_state'];
    }
}

Not certain if it matters, but the warning is thrown on the 'if'. As far as I am aware, every 'fetch' is now a 'fetchAll' and includes a 'closeCursor'.

The connection is being set like this:

$this->dbh = new PDO($dsn, $user, $password, array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY =>true
            ));

Suggestions?

  • You don't use `fetchAll` in a `while` loop. `fetchAll` already retrieves all rows into an array. What PDO error mode are you using? – Phil Aug 08 '13 at 05:39
  • I am using: `PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING`. As I mentioned I was originally using `fetch`; after dumping the results of `fetchAll` I realized that the while wasn't going to fit the structure, but I didn't realize that the `while` was not needed. Thanks. – user2663120 Aug 08 '13 at 12:05
  • UPDATE: I have removed all of the `while` loops and closed the cursor immediately after the `fetchAll`. In addition, I have (re)set the `PDO::MYSQL_ATTR_USE_BUFFERED_QUERY` to true. I am still receiving the 2014 warning. How is this possible? What am I missing? Maybe I should note that the app is executing some sql via the local command line? I would think this is a separate connection and wouldn't impact the execution of the rest of the app. – user2663120 Aug 08 '13 at 16:02
  • Use `PDO::ERRMODE_EXCEPTION`. I'd say this query isn't your problem. You most likely have another unbuffered query that is still active somewhere. – Phil Aug 08 '13 at 23:04
  • OK - that was helpful... I think! =) Changing the attribute listed a fatal error on the original offending query (same 2014 error). This query is part of an object that is instantiated at the top of the script - after a truncate is performed on several tables. Instantiating above the truncate avoids the error. Now I am back to an error on the posted query. What I don't understand is... isn't the buffering set to true supposed to take care of all this? Thanks for the help! – user2663120 Aug 08 '13 at 23:31

2 Answers2

2
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);

is the silver bullet to fix the error, which text you didn't even bothered to provide, supposing readers to know every error by numbers by heart.

While just mechanically replacing all the fetch() calls to fetchAll() is not.

Two things on your edited code.

  1. You have quite useless loop. As I said above, doing something mechanically, mindlessly, can bring no good. One have to have at least minimal understanding. The loop you have is useless twice: because $row you have from fetchAll() don't need any looping over (it already contain all the data) and because you are getting only one row anyway and no loop ever needed. And thus fetch() should be used not fetchAll.
  2. Despite of your feeling, not "every 'fetch' is now a 'fetchAll'" as the error message clearly tells us.
  3. It seems you are selecting rows one by one inside of a larger loop. And it apparently smells of some wrong query. You have to make this (invisible to us) outer query to get all the data at once, using fetchAll() and without all these inner queries.
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Ok - I did in fact try setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY to true, prior to resorting to fetchAll, but there was no change. – user2663120 Aug 08 '13 at 11:59
  • I am afraid I don't understand: I was using `while` loops with `fetch` on all queries. When I attempted to introduce one more query for a new class the object failed to initiate properly (the `if` conditional was not true) leaving no data to be read. First I changed the `PDO::MYSQL_ATTR_USE_BUFFERED_QUERY` to true. It didn't work. Why? How can that be possible? I don't know. The error suggests that `fetch` be changed to `fetchAll`. I have opened up every referenced document and searched for and changed every `fetch`. Error. Critique the code if you must; but I still don't know what to do. =/ – user2663120 Aug 09 '13 at 04:43
  • I suppose I should say that I have not posted this to have the code critiqued as much as I am trying to illustrate what has been done in an attempt to solve the problem. I would of expected setting the `PDO::MYSQL_ATTR_USE_BUFFERED_QUERY` to true would of solved the problem. It didn't. How is that possible? What does that mean? That there is a non-buffered query somewhere? But how? All the connections use the same attributes. Set the `fetch` to `fetchAll`. Unbuffered query. How? What is left? That a PDO connection is established that is not using the set attributes? How is that possible? – user2663120 Aug 09 '13 at 05:22
0

I think that it could be a PHP bug.

https://bugs.php.net/bug.php?id=57540

What is your PHP version?

sectus
  • 15,605
  • 5
  • 55
  • 97