Advertisement:

Author Topic: What is better to reduce memory usage?  (Read 134 times)

fog

  • Hero Member
  • *****
  • Posts: 1062
What is better to reduce memory usage?
« on: April 09, 2019, 04:39:11 am »
What is better to reduce memory usage?

Method 1

simple SQL query to display all table results
Using several foreach loops from the same query... good or bad?

And using foreach loops to use statements inside and print values cannot be a good idea?
For other hand, using to return values, etc.

example:

Code: [Select]
function my_query() {
//code... result...
return $variable;
}

foreach(my_query() as $something){
//... do something here
}

//... and so on

Method 2

Using Multiple SQL queries limited to display entries of a certain specific ID repeated/copied on a table
Using several foreach loops to different SQL queries

example:

Code: [Select]
function my_custom_query() {
//code... limit query result... or specific columns
return $custom;
}

//... and so on with multiple query functions

foreach(my_custom_query() as $something){
//... do something here
}

//... and so on with multiple foreach loops to other custom queries

Observations:

All run ok anyway, but what is the good method to keep all with less memory?


questions:

Using multiple foreach loops can be a bad practice, and no matters if we can used from a unique query to all foreach loops?
That method can kill php memory, or will depend with something to contour that?
array_slice() will only filter the results or permanently limit the array, running on first two rows and stop?
Code: [Select]
foreach(array_slice($articles, 0, 2) as $row ){...}
Thank you for your replay here with your experience
Regards