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.