W
W
WEBIVAN2013-08-11 15:37:17
PHP
WEBIVAN, 2013-08-11 15:37:17

PHP+PDO Mysql, query not executed after exec with multiple queries inside?

After executing a request like

$db->exec('UPDATE as SET b=c WHERE d=1; UPDATE as SET b=e WHERE d=2; UPDATE as SET b=f WHERE d=3');<br>

attempt to execute returns false instead of PDOStatement. $db->errorInfo() returns
$db->query('SELECT * FROM e') ;<br>
Array<br>
(<br>
    [0] => HY000<br>
    [1] => 2014<br>
    [2] => 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.<br>
)<br>

As far as I understand, multiple requests in exec pass the result of each execution to the PDO buffer, which must be cleared via fetchAll (). That's just the PDOStatement object is not returned to me after exec, and it shouldn't be, according to the documentation.
Is it possible to execute several requests with one exec, so that the buffer would not fill up later?
Or is it possible somehow to clear this buffer after performing several requests through one exec?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WEBIVAN, 2013-08-11
@WEBIVAN

The solution turned out to be obvious. Instead of

$db->exec('UPDATE as SET b=c WHERE d=1; UPDATE as SET b=e WHERE d=2; UPDATE as SET b=f WHERE d=3');

write
$db->query('UPDATE as SET b=c WHERE d=1; UPDATE as SET b=e WHERE d=2; UPDATE as SET b=f WHERE d=3')->fetchAll(PDO::FETCH_NUM);

A
Alexander, 2013-08-12
@kryoz

I would prefer to use PDOStatement anyway.
$sth = $db->prepare($sql);
$sth->execute($params);
$sth->closeCursor();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question