开发者

php PDOStatement fetchAll second time

开发者 https://www.devze.com 2023-02-15 00:31 出处:网络
I do not really understand the point of PDOStatement since: $PDO = new PDO(); $PDOS = $PDO->query($sql);

I do not really understand the point of PDOStatement since:

$PDO = new PDO(); 
$PDOS = $PDO->query($sql);

var_dump($PDOS->fetchAll()); //will return开发者_C百科 data 
var_dump($PDOS->fetchAll()); //empty

Is there a param that needs to be passed so that 2nd time fetchAll returns data, but without executing the SQL again?


Just store the result the first call to fetchAll() into a PHP variable. Any reason you can't do that?

$results = $PDOS->fetchAll();

Then you can use $results as much as you need to without taxing your database any further.


The PDOStatement has an iterator.

PDOStatement::fetch() will iterate over the rowset. When calling fetchAll() the iterator is at the last row.

This iterator moves only in 1 direction. The only method to go back is to execute the query again. This is the nature of the database and PHP should not keep the entire rowset in memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消