In PHP, when I call a MySQL
stored procedure
using PDO
, and then another PDO
query, just like this:
$dbh = new PDO('mysql:host=localhost;dbname=db1','user1','password1');
$query = "CALL get_token()";
$stmt = $dbh->query($query);
$array = $stmt->fetchAll();
$query = "SELECT * FROM `table1`";
$stmt = $dbh->query($query);
$array = $stmt->fetchAll();
The MySQL
stored procedure
is about like this:
CREATE PROCEDURE `get_token`()
BEGIN
DECLARE token CHAR(64);
DECLARE expire SMALLINT;
SELECT `token`, `expire` INTO token, expire FROM `token`;
SELECT token, expire;
END$$
And I got the following error message (using try...catch
to catch it):
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.
Even if I followed the instructions described in the above error message (that means using fetchAll()
and setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY
attribute), I still got the same error message.
If I change the first query to a normal SELECT
SQL query, instead of a stored procedure
, I won't get this error. So it seems that the problem arises from the stored procedure
.
But how can I fix this?