Handling big data sets from MySQL with PHP: mysql_unbuffered_query

It’s good to know that mysql_query fetches and buffers the result rows in memory. This can cause memory exhausted problems, prematurely ended scripts and so on and so forth. While reading this interesting article on lazy evaluation (go ahead and read it, there is a nice digression on different languages such as Haskell and Python), I found out about the existance of mysql_unbuffered_query which fixes this exact problem. If you are part of the cool guys who use PDO and its derivatives, unbuffered queries are the default. If for some insane reason you need to buffer them, you can use array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true) as a param to your prepare statements.