How to solve the problem of memory exhaustion when a large number of data loops in PHP

Source: Internet
Author: User
Tags mysql php website query return

I recently encountered the following error while developing a PHP program:

  
   
   
  1. PHP Fatal error:allowed Memory size of 268 435 456 bytes Exhausted

The error message shows that the maximum allowable memory has been exhausted. I was surprised at first by such a mistake, but in the blink of an eye, it is not surprising, because I am developing the program is to use a foreach circular statement in a 40,000-record table search for a specific feature of the data, that is, one time to take out 40,000 data, and then check each day data. It can be imagined that 40,000 data are all loaded into memory, memory does not explode to blame.

After all the programming for so many years, I vaguely remember that PHP provides an API that does not load all of the data at once, and is a query method that, as with streaming media, is used with lost data and does not accumulate in memory. After a simple search, sure enough, the correct usage was found on the official website.

The problem is called the buffer and non-buffering queries (buffered and unbuffered queries) on the official PHP website. PHP's query default mode is buffering mode. In other words, the query data will be extracted all at once to the memory for the PHP program processing. This gives the PHP program extra functionality, such as counting the number of rows, pointing the pointer at a row, and so on. More importantly, the program can repeatedly two times query and filter data set operations. But this kind of buffer query pattern flaw is consumes the memory, namely uses the space to change the speed.

In contrast, another type of PHP query mode is not buffer query, the database server will be a piece of the return data, rather than return all at once, the result is that the PHP program consumes less memory, but it increases the pressure of the database server, because the database will be waiting for PHP to fetch data, Until all the data has been taken out.

Obviously, the buffer query mode is suitable for small data query, but not buffer query is suitable for large amount of data query.

For the PHP buffer mode query, as you know, the following example illustrates how to execute a non-buffered query API.

Non-buffered Query method one: mysqli

 
  
  
  1. <?php
  2. $mysqli = new Mysqli ("localhost", "My_user", "My_password", "World");
  3. $uresult = $mysqli->query ("SELECT Name from City", Mysqli_use_result);
  4. if ($uresult) {
  5. while ($row = $uresult->fetch_assoc ()) {
  6. echo $row [' Name ']. Php_eol;
  7. }
  8. }
  9. $uresult->close ();
  10. ?>

Non-Buffered Query method two: Pdo_mysql

 
  
  
  1. <?php
  2. $pdo = new PDO ("Mysql:host=localhost;dbname=world", ' my_user ', ' my_pass ');
  3. $pdo->setattribute (Pdo::mysql_attr_use_buffered_query, false);
  4. $uresult = $pdo->query ("SELECT Name from City");
  5. if ($uresult) {
  6. while ($row = $uresult->fetch (PDO::FETCH_ASSOC)) {
  7. echo $row [' Name ']. Php_eol;
  8. }
  9. }
  10. ?>

Non-Buffered Query method three: MySQL

 
  
  
  1. <?php
  2. $conn = mysql_connect ("localhost", "My_user", "My_pass");
  3. $db = mysql_select_db ("World");
  4. $uresult = Mysql_unbuffered_query ("SELECT Name from City");
  5. if ($uresult) {
  6. while ($row = Mysql_fetch_assoc ($uresult)) {
  7. echo $row [' Name ']. Php_eol;
  8. }
  9. }
  10. ?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.