A method of querying a database using non-buffered mode in PHP

Source: Internet
Author: User
Buffer queries and non-buffered queries (Buffered and unbuffered queries). PHP's query default mode is buffer mode. In other words, the query data results will be extracted to the memory for the PHP program processing, the need for friends can refer to the following

The following error has been encountered in developing a PHP program recently:

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, but I thought it was not surprising, because I was developing this program to use a Foreach Loop statement in a table with 40,000 records to search for a specific feature of the data, that is, to take 40,000 data at a time, and then check each day data. It can be imagined that 40,000 of all the data loaded into memory, the memory is not explosive only strange.

After all, after all these years of programming, I vaguely remember that PHP provides an API that does not load all the data at once, and it is a query method that, as with streaming media, is dropped with random, data does not accumulate in memory. After a simple search, sure enough, the correct usage is found on the official website. Buffer queries and non-buffered queries (Buffered and unbuffered queries). 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 in this buffered query pattern is that it consumes memory.

Another kind of 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 that the PHP program consumes less memory, but increases the pressure on the database server, because the database will be waiting for PHP to fetch data, until the data are all taken out.

Non-buffered Query method one: mysqli


<?php$mysqli = new Mysqli ("localhost", "My_user", "My_password", "World"); $uresult = $mysqli->query ("Select Name From the 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.