Workaround for PHP queries for large data memory exhaustion problems

Source: Internet
Author: User
Tags php website
This article mainly for you in detail the PHP query a large number of data memory exhaustion problem solution, with a certain reference value, interested in small partners can refer to

When querying large amounts of data from a database, there is a hint of insufficient content :

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

This issue is called buffer queries and non-buffered queries (Buffered and unbuffered queries) on the official PHP website. PHP's query default mode is buffer mode. In other words, the results of the query data are extracted to the memory for the PHP program processing. This gives the PHP program extra functionality, such as calculating the number of rows, pointing a pointer to a row, and so on. What's more, the program can perform two queries and filters on the data set repeatedly. But the flaw of this buffer query mode is to consume memory, that is, to use space for speed.

In contrast, the other PHP query mode is a non-buffered query, the database server will return a single piece of data, rather than return all at once, the result is a PHP program consumes less memory, but increases the pressure on the database server, because the database will wait for PHP to fetch data, Until the data is all taken out.

It is clear that the buffered query pattern is suitable for small data volume queries, and non-buffered queries are adaptable to large data volume queries.

For PHP's buffered mode queries, we all know that the example below is how to execute the non-buffered query API.

non-Buffered Query method one: mysqli


<?php $mysqli = new mysqli ("localhost", "My_user", "My_password", "World"); $uresult = $mysqli->query ("Select Name from City", Mysqli_use_result); if ($uresult) {while   ($row = $uresult->fetch_assoc ()) {     echo $row [' Name ']. Php_eol;   } } $uresult->close ();?>


non-Buffered Query method two: Pdo_mysql


<?php $pdo = new PDO ("Mysql:host=localhost;dbname=world", ' my_user ', ' my_pass '); $pdo->setattribute (Pdo::mysql_attr_use_buffered_query, false); $uresult = $pdo->query ("Select Name from City"); if ($uresult) {while   ($row = $uresult->fetch (PDO::FETCH_ASSOC)) {     echo $row [' Name ']. Php_eol;   } }?>


Non-Buffered Query method three: MySQL


<?php $conn = mysql_connect ("localhost", "My_user", "My_pass"); $db  = mysql_select_db ("World"), $uresult = Mysql_unbuffered_query ("Select Name from City"), if ($uresult) {   while ($row = Mysql_fetch_assoc ($uresult)) {     echo $row [' Name ']. Php_eol;   } }?>


The above is the whole content of this article, I hope that everyone's study has helped.


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.