我试图一次从数据库中检索一行,因为我想检索一段时间(特别是用于调度),以便在PHP代码中进行处理。当我执行()和fetchAll()一次时,它工作得很好。当我将这两个函数放置在上述for循环中时,第一次迭代按预期工作(给定所述参数检索正确的行)。但是对于下面的迭代,它不再返回任何值。
请告诉我在使用PDO方面的缺陷。我是PHP的初学者,并且仍然在关注面向对象的数据库访问。
$connection = OpenCon('localhost', 'prof', 'prof_pass', 'school_db'); //Open PDO connection
$query_class = $connection->prepare("CALL school_db.get_class_block(?, ?);");
$query_class->bindParam(1, $day_date_string);
$query_class->bindValue(2, $prof_id);
for ($col = 0; $col < 7; $col++) {
for ($row = 0; $row < 9; $row++) {
$day_date_string = $day_date->format('Y-m-d H:i:s');
echo "<br/>$day_date_string<br/>";
$query_class->execute();
$m = $query_class->fetchAll();
echo count($m);
$day_date->add($date_span->createFromDateString('90 minutes')); //Add 1.5 hours
}
$day_date->add($date_span->createFromDateString('10 hours + 30 minutes')); //Add 9.5 hours to switch the next day
}
CloseCon($connection); //Close connection发布于 2019-09-16 21:33:48
由于MySQL的quirk,当查询是CALL语句时,试图重用该语句将导致以下错误:
带有消息“PDOException: General : 2014不能在其他未缓冲的查询处于活动状态时执行查询”的默认异常“SQLSTATEHY000”。请考虑使用PDOStatement::fetchAll()。或者,如果您的代码只对mysql运行,则可以通过设置PDO::MYSQL_ATTR_USE_BUFFERED_QUERY属性启用查询缓冲。
错误消息中的任何建议实际上都不起作用。但是在调用execute()之前关闭游标可以解决这个问题。
for ($col = 0; $col < 7; $col++) {
for ($row = 0; $row < 9; $row++) {
$day_date_string = $day_date->format('Y-m-d H:i:s');
echo "<br/>$day_date_string<br/>";
$query_class->execute();
$m = $query_class->fetchAll();
$query_class->closeCursor();
echo count($m);
$day_date->add($date_span->createFromDateString('90 minutes')); //Add 1.5 hours
}
$day_date->add($date_span->createFromDateString('10 hours + 30 minutes')); //Add 9.5 hours to switch the next day
}https://stackoverflow.com/questions/57964148
复制相似问题