例子:
$query = $mydb->query('PRAGMA table_info("mytable")');
//variable query has now type of PDOStatement Object
print_r($query->fetchAll(PDO::FETCH_COLUMN,1)); // this result is ok
print_r($query->fetchAll(PDO::FETCH_COLUMN,2)); // but this result gives empty array
那么有什么方法可以重用语句对象吗?
最佳答案
当没有更多行可用时,PDOStatement 对象返回 false。对 fetchAll 的调用涵盖了所有行,这些行将在随后的任何提取尝试中始终返回 false。这限制了语句对象的重用。您可以使用 PDOStatement::bindColumn 来实现您在示例中尝试的效果。
$query = $mydb->query('PRAGMA table_info("mytable")');
// Variables passed to be bound are passed by reference.
// Columns are based on a *1* index or can be referenced by name.
$query->bindColumn(1, $bound_column1);
$query->bindColumn(2, $bound_column2);
$query->fetchAll();
print_r($bound_column1);
print_r($bound_column2);
关于php - 是否可以在 PDOStatement 对象的单个实例上执行多个 fetch() 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6713013/