$stmt = $pdo->query('SELECT * FROM table'); $data = $stmt->fetchAll(); print json_encode($data); 

Allowed memory size of 268435456 bytes exhausted (tried to allocate 28 bytes).

How to solve this problem?

  • 2
    You just need absolutely all the data from the table table - Naumov
  • @Naumov is just interesting) When I used the mysql methods, the values ​​were given out completely, and here it was) - Martinez Toni
  • one
    You pull out the entire table in memory, memory crashes down - etki
  • Try var_dump($data) to start with, you may have fetchAll with an error running and where execute for the article. - Naumov
  • @Naumov, I mean with an error) Everything works the same) - Martinez Toni 2:51 pm

2 answers 2

At the beginning of the script

 ini_set('memory_limit','2G'); 

but I would think about how to rework the algorithm of the web-script too much.

If page loading then you need to get as many elements as there are on the page. This is a bad option for web scripts. Because the memory is not rubber and reserve so much memory is bad for the execution of the script. For it will not be available for other applications and 5-6 (simultaneous) script launches will simply tumble down your server ... This option is not suitable for a less-loaded project.

  • helped) Thank you) At the initial stage you need to get full) In the future, my client will have a page-by-page loading, and as I understand it, I will need to change the structure of the request) - Martinez Toni
  • those. If page loading then you need to get as many elements as there are on the page. This is a bad option for web scripts. Because the memory is not rubber and reserve so much memory is bad for the execution of the script. For it will not be available for other applications and 5-6 (simultaneous) script launches will simply tumble down your server ... This option is not suitable for a less-loaded project - Naumov
  • If it helped, then mark the answer as correct. - Naumov

How to solve this problem?

Do not make queries like SELECT * FROM table , but always request only the data that is needed to display on the page.

And even more so do not use the monstrous crutches from another answer.

  • You probably never finish reading the answers to the end. Plus, your answer is not the answer to the question. - Naumov
  • one
    @Naumov memory leak may well be associated with a query that returns the entire table. The tables are big. And instead of optimizing, you offer just to give more memory. This is somehow short-sighted. - Nick Volynkin 4:19 pm
  • @NickVolynkin I wrote in my answer that it is a bad idea to give the web script more memory. What does my comment say If page loading then you need to get as many elements as there are on the page. This is a bad option for web scripts. Because the memory is not rubber and reserve so much memory is bad for the execution of the script. For it will not be available for other applications and 5-6 (simultaneous) script launches will simply tumble down your server ... This option is not suitable for the less loaded project `and the last lines of the answer. - Naumov
  • @NickVolynkin And if you think that it’s quite normal for the CO to respond fully, then the person suggested a specific solution to a specific question and warned the person about not submarine rooms of such an answer, И тем более не применять чудовищные костыли из другого ответа. and while the answer does not contain any specific proposals, I will not argue with you. I'm just in my comments to the answers will refer to your comment in this topic and tell people что они пишут кастыли, и быдлокодеры that I would not be banned for it. In general, there will be a full answer, etc. - Naumov
  • 2
    @Naumov "crutches" is a characteristic of the solution, not of a person. Not at all the same as "bydlokoder". - Nick Volynkin pm