In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to solve the problem of running out of memory when a large number of data loops in PHP, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Recently, I encountered the following error while developing a PHP program:
PHP Fatal error: Allowed memory size of 268 435 456 bytes exhausted
The error message indicates that the allowed memory has been exhausted. I was surprised at first to encounter such an error, but on second thought, it is not surprising, because the program I am developing is to use a foreach loop statement to search for data with specific characteristics in a table with 40, 000 records, that is, to take out 40, 000 pieces of data at a time, and then check the daily data one by one. It is conceivable that 40,000 pieces of data are all loaded into memory, no wonder the memory does not explode.
After all, after so many years of programming, I vaguely remember that PHP provides an API that loads data not all at once, which is a query method like streaming media, which takes whatever you want and does not accumulate data in memory. After a simple search, the correct usage was found on the official website.
This problem is called buffered query and unbuffered query (Buffered and Unbuffered queries) on PHP's official website. The default query mode for PHP is buffering mode. In other words, the results of the query data are all extracted into memory at one time for the PHP program to process. This gives the PHP program additional functions, such as counting lines, pointing the pointer to a line, and so on. More importantly, the program can repeatedly query and filter the data set. But the drawback of this buffered query mode is the consumption of memory, that is, space for speed.
In contrast, another PHP query mode is an unbuffered query, in which the database server returns the data one by one, rather than all at once. The result is that the PHP program consumes less memory, but increases the pressure on the database server, because the database waits for PHP to fetch the data until it is all fetched.
Obviously, the buffered query mode is suitable for small data query, while non-buffered query is suitable for large data query.
As you all know about PHP's cached mode query, the example listed below is how to execute the unbuffered query API.
Unbuffered query method 1: mysqli
Unbuffered query method 2: pdo_mysql
Unbuffered query method 3: mysql
The above content is how to solve the problem of running out of memory when a large number of data loops in PHP. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.