Parham Doustdar
Parham Doustdar

Reputation: 2039

How does PDOStatement::fetch() work internally?

I read everywhere that using PDOStatement::fetch() would mean you will not run out of memory, no matter how large the resultset is. So that begs the question: where are the rows stored?

PHP has to store the results somewhere when it gets them from the database. Where are these results stored? Or, are they stored in the database, and PHP has to query the database for the next row every time?

Upvotes: 1

Views: 77

Answers (2)

Max Romanenko
Max Romanenko

Reputation: 86

It's similar to reading a file, you open a stream and read data piece by piece, except that database internals are a way more complicated.

For example, look at the description of $driver_options PDO::prepare, so you can even set scrollable cursor in order to control the direction of reading.

Upvotes: 1

Justinas
Justinas

Reputation: 43479

PDOStatement::fetch will get you next row from your query. I don't think that it will not run out of memory (if one row contains lots of data) because your data will be held in memory (read about Buffered and Unbuffered queries).

Upvotes: 0

Related Questions