|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55003 PDO prepare() ignores MYSQL_ATTR_USE_BUFFERED_QUERY
Submitted: 2011-06-06 20:15 UTC Modified: 2013-02-18 00:34 UTC
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: jamesvl+php at gmail dot com Assigned:
Status: No Feedback Package: PDO related
PHP Version: 5.3.6 OS: all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-06-06 20:15 UTC] jamesvl+php at gmail dot com
Steps to reproduce:

Set up a database so a query can return ~ 10,000 rows - enough to bump memory usage to noticeable levels.

Use PDO to prepare a query where you explicitly disable buffered queries: PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false. The PHP docs give an example of how to do this at

Execute that statement.

Use "memory_get_peak_usage()" to watch PHP memory usage for the script before and after the call to pdoStmt->execute().


Settings PDO::MYSQL_ATTR_USE_BUFFERED_QUERY to false when creating the PDO object *does* work as expected. (i.e. query buffering will be turned off or on; default is on if not specified)

However, the PHP documentation makes it appear that an individual prepared statement should be able to change or override this settings for an individual query, which is not the case.

PHP version tested: PHP 5.3.6 using mysqlnd, Win32 and Linux custom compile.
Note that testing on PHP 5.2 (i.e. without mysqlnd) is not possible since mysqlib allocates memory outside of the PHP parser, and thus is not tracked by memory_get_peak_usage() and is not limited by php.ini memory_limit settings.

Test script:
if ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') {
   $stmt = $this->db->prepare($qry,
      array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false));

   echo "\nmax bytes mem used: " . memory_get_peak_usage() . "\n";
   echo "max bytes mem used: " . memory_get_peak_usage() . "\n";

Expected result:
Expect to see:

Very little new memory allocated even when returning large result sets.

Memory usage should *not* vary based on the result set size.

max bytes mem used: 700624
max bytes mem used: 700624

Actual result:
Actually see:

Memory usage increases directly with the size of the result set returned - PDO is using a buffered query.

max bytes mem used: 700240
max bytes mem used: 26291600


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-04 09:16 UTC]
Please try using this snapshot:
For Windows:

 [2012-05-04 09:16 UTC]
-Status: Open +Status: Feedback
 [2013-02-18 00:34 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Aug 13 19:05:45 2022 UTC