


How to solve the problem of memory exhaustion when looping a large amount of data in PHP_PHP Tutorial
How to solve the problem of memory exhaustion when looping a large amount of data in PHP
I recently encountered the following error when developing a PHP program:
<ol class="dp-j"><li class="alt"><span><span>PHP Fatal error: Allowed memory size of </span><span class="number">268</span><span> </span><span class="number">435</span><span> </span><span class="number">456</span><span> bytes exhausted </span></span></li></ol>
The error message shows that the maximum allowed memory has been exhausted. I was surprised to encounter such an error at first, but after thinking about it, it is not surprising, because the program I am developing is to use a foreach
loop statement to search the entire table in a table with 40,000 records. For data with specific characteristics, that is to say, 40,000 pieces of data must be taken out at a time, and then the daily data must be checked one by one. It is conceivable that if all 40,000 pieces of data are loaded into the memory, it would be strange if the memory does not burst.
After all these years of programming, I vaguely remember that PHP provides an API that does not load data all at once. It is a query method that can be used and lost like streaming media, and the data does not accumulate in the memory. After a simple search, I found the correct usage on the official website.
This problem is called Buffered and Unbuffered queries on PHP’s official website. PHP's default query mode is buffered mode. In other words, the query data results will be extracted into memory all at once for processing by the PHP program. This gives the PHP program additional functions, such as counting the number of rows, pointing the pointer to a certain row, etc. What's more important is that the program can repeatedly perform secondary queries and filtering operations on the data set. However, the disadvantage of this buffered query mode is that it consumes memory, that is, it trades space for speed.
In contrast, another PHP query mode is a non-buffered query. The database server will return data one by one instead of all at once. The result is that the PHP program consumes less memory, but it increases the load of the database server. Pressure, because the database will keep waiting for PHP to fetch the data until all the data is fetched.
Obviously, the buffered query mode is suitable for queries with small data volumes, while non-buffered queries are suitable for queries with large data volumes.
Everyone knows about PHP’s buffered mode query. The example listed below is how to execute the non-buffered query API.
Non-buffered query method one: mysqli
<ol class="dp-j"><li class="alt"><span><span><?php </span></span></li><li><span>$mysqli = <span class="keyword">new</span><span> mysqli(</span><span class="string">"localhost"</span><span>, </span><span class="string">"my_user"</span><span>, </span><span class="string">"my_password"</span><span>, </span><span class="string">"world"</span><span>); </span></span></li><li class="alt"><span>$uresult = $mysqli->query(<span class="string">"SELECT Name FROM City"</span><span>, MYSQLI_USE_RESULT); </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span> ($uresult) { </span></span></li><li><span> <span class="keyword">while</span><span> ($row = $uresult->fetch_assoc()) { </span></span></li><li class="alt"><span> echo $row[<span class="string">'Name'</span><span>] . PHP_EOL; </span></span></li><li><span> } </span></li><li class="alt"><span>} </span></li><li><span>$uresult->close(); </span></li><li class="alt"><span>?> </span></li></ol>
Non-buffered query method two: pdo_mysql
<ol class="dp-j"><li class="alt"><span><span><?php </span></span></li><li><span>$pdo = <span class="keyword">new</span><span> PDO(</span><span class="string">"mysql:host=localhost;dbname=world"</span><span>, </span><span class="string">'my_user'</span><span>, </span><span class="string">'my_pass'</span><span>); </span></span></li><li class="alt"><span>$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, <span class="keyword">false</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>$uresult = $pdo->query(<span class="string">"SELECT Name FROM City"</span><span>); </span></span></li><li><span><span class="keyword">if</span><span> ($uresult) { </span></span></li><li class="alt"><span> <span class="keyword">while</span><span> ($row = $uresult->fetch(PDO::FETCH_ASSOC)) { </span></span></li><li><span> echo $row[<span class="string">'Name'</span><span>] . PHP_EOL; </span></span></li><li class="alt"><span> } </span></li><li><span>} </span></li><li class="alt"><span>?> </span></li></ol>
Non-buffered query method three: mysql
<ol class="dp-j"><li class="alt"><span><span><?php </span></span></li><li><span>$conn = mysql_connect(<span class="string">"localhost"</span><span>, </span><span class="string">"my_user"</span><span>, </span><span class="string">"my_pass"</span><span>); </span></span></li><li class="alt"><span>$db = mysql_select_db(<span class="string">"world"</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>$uresult = mysql_unbuffered_query(<span class="string">"SELECT Name FROM City"</span><span>); </span></span></li><li><span><span class="keyword">if</span><span> ($uresult) { </span></span></li><li class="alt"><span> <span class="keyword">while</span><span> ($row = mysql_fetch_assoc($uresult)) { </span></span></li><li><span> echo $row[<span class="string">'Name'</span><span>] . PHP_EOL; </span></span></li><li class="alt"><span> } </span></li><li><span>} </span></li><li class="alt"><span>?> </span></li></ol>


APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
