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();

我试过$results->buffer();&& $results->rewind()

我的迭代代码就像

foreach ( $results as $result ){    
       // code goes here ..
}
....

foreach ( $results as $result ){    
       // code goes here ..
}
4

2 回答 2

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();
于 2013-02-15T12:14:13.910 回答
4

您看到的异常说:

这个结果是只向前的结果集,不支持向前移动后调用rewind()

因此,简单的解决方案是将结果存储在一个数组中,然后您可以多次迭代该数组。

$records = array();

foreach ($results as $result)
{
    $records[] = $result;
}
于 2013-02-15T12:15:46.780 回答