获取档案<{1}}
Zend\Db\Adapter\Exception\RuntimeException
我的数据库设置代码是
\library\Zend\Db\Adapter\Driver\Pdo\Result.php:159
结果提取代码是
array(
'db' => array(
'driver' => 'Pdo',
'pdodriver' => 'mysql',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
'buffer_results' => true
),
'username' => 'root',
'password' => '',
'host' => 'localhost',
'dbname' => 'db_name',
),
)
我试过$statement = $this->sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
&amp;&amp; $results->buffer();
我的迭代代码就像
$results->rewind()
答案 0 :(得分:9)
这是因为PDO结果集是Forward Only(因此您无法回放)。
您可以尝试使用ResultSet包装它:
$results = $statement->execute(); // generate your results
$resultSet = new ResultSet; // Zend\Db\ResultSet
$resultSet->initialize($results);
// You should now be able to iterate over this multiple times..
foreach($resultSet as $row) {
// ..
}
// You can now buffer if you need..
$resultSet->buffer();
// Or get an array of all items
$arrayOfResults = $resultSet->toArray();
答案 1 :(得分:4)
您看到的例外情况说:
此结果是仅向前结果集,之后调用rewind() 不支持前进
因此,简单的解决方案是将结果存储在数组中,然后您可以多次遍历该数组。
$records = array();
foreach ($results as $result)
{
$records[] = $result;
}