我一直在尝试使用PDO从存储过程中获取结果集,但是当前对数据库进行多次调用时会出现错误
SQLSTATE[HY000]: 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
代码:
$table = $data["TABLE_NAME"];// this returns table names like table_1,table_2
$exc = $conn->prepare("CALL Dummy_2('$table')");
$exc->execute();
while($finalRes = $exc->fetch(PDO::FETCH_ASSOC)) {
$ID = substr($table,11);
$exc2 = $conn->prepare("CALL sp_new('$ID')");
$exc2->execute();// the place which triggers the error
if(false !== $result) {
$totals = array();
while($row = $exc2->fetch(PDO::FETCH_ASSOC)) {
$tot = new stdClass();
$tot->count = (int)$row['cnt'];
$tot->ucount = (int)$row['ucnt'];
$tot->date = new DateTime($row['dat']);
$totals[] = $tot;
}
var_dump($tot);
}
}
答案 0 :(得分:2)
您正在尝试第二个预备语句($exc2
),而同一连接上已有一个语句正在进行中。与警告建议一样,尝试使用fetchAll
并循环返回数据,而不是一次一行地获取行 - 这样,您可以在开始第二个语句之前关闭第一个语句。
示例强>
我没有时间测试它,但您可以尝试以下方法。我已更改第4-5行($dataset
和foreach
)。
$table = $data["TABLE_NAME"];// this returns table names like table_1,table_2
$exc = $conn->prepare("CALL Dummy_2('$table')");
$exc->execute();
$dataset = $ex->fetchAll();
foreach($dataset AS $finalRes) {
$ID = substr($table,11);
$exc2 = $conn->prepare("CALL sp_new('$ID')");
$exc2->execute();// the place which triggers the error
if(false !== $result) {
$totals = array();
while($row = $exc2->fetch(PDO::FETCH_ASSOC)) {
$tot = new stdClass();
$tot->count = (int)$row['cnt'];
$tot->ucount = (int)$row['ucnt'];
$tot->date = new DateTime($row['dat']);
$totals[] = $tot;
}
var_dump($tot);
}
}