views:

23

answers:

1

I never understood this: in web development whe[n|re] does it make sense to fetch results one by one?

I mean, why should I use PDOStatement->fetch*() when I can use PDOStatement->fetchAll()?

+1  A: 

fetchAll() will fetch all the results into one big array.

With very large result sets, it could exceed the PHP script's memory limit.

A pure fetch() will fetch each record one by one, neutralizing that danger.

That's the only reason not to use fetchAll() I can think of.

Pekka
Sure, but we usually don't emit SQL statements like `SELECT * FROM oneMillionRowsTable` without a limit clause, at least I've never seen a query like that in web applications.
Alix Axel
@Alix neither have I, but it is not entirely unthinkable to happen. I prefer the `fetch` variant out of a general sense of "My PHP script's memory is more limited than that of the mySQL server". It could be, though, that `fetchAll` is faster because it talks to the server only once - I don't know, I've never measured it.
Pekka
Thanks Pekka. =)
Alix Axel