1

As the title suggests, I am looking to get some explanation about which of these methods would be best performance wise. I would think that #2 would be better because it uses only one loop instead of a while loop and a for loop. Is there even any point in using method #1?

The first method

// assign data from row to $users[]
while($row = mysqli_fetch_assoc($result))
{
    $users[] = $row;
}
// and later on use a for loop to access the data in $users[]
for($i=0; $i < count($users); $i++)
{
    echo $users[$i]['name'] . "<br>";
}

Method 2:

// only using a while loop
while ($row = mysqli_fetch_assoc($result)) 
{
    echo $row['name'] . "<br>";
}
8
  • 1
    If you want to access the array outside of the loop, the first method might be useful. But 99% of the time you're going to want to use the second method. May 14, 2017 at 21:16
  • 1
    You are using the same method, except that in the first code you are keeping the data stored in an array for future access, while in the second you just print it right away and discard the variable. If you don't need to keep the data around the second one is preferred of course. Also, you should start using OOP to keep the code clearer, use $result->fetch_assoc() instead of mysqli_fetch_assoc($result).
    – Havenard
    May 14, 2017 at 21:19
  • That's what I thought too. Do you have any idea about the differences in performance at larger scale?
    – Eddy Vinck
    May 14, 2017 at 21:19
  • @Havenard I am going to learn OOP in PHP soon. I already know how to do it in other languages such as c# so it shouldn't be too hard assuming they work in a similar way.
    – Eddy Vinck
    May 14, 2017 at 21:21
  • 1
    You will use more memory in the first code but unless you are fetching a million records from the database it shouldn't be alarming. Beside that there are no major performance issues that deserve to be addressed You could optimize the for loop by using foreach so that you don't call count() n times, but that's minor.
    – Havenard
    May 14, 2017 at 21:21

2 Answers 2

1

Second method is better if by performs better you mean more optimal. It's less intense on memory and cpu. First method makes your script to save all needed data to array from database, so you need some memory to "cache" it, and it also takes little of cpu time to rewrite and than read it. Of course those aren't big numbers.

But while that method performs better in regard of pure optimization, the first method is better if you want to use the fetched data to do something more intresting with it, but if that is for example printing record's from DB and than forgetting, it's not worth the wasted resources.

0

It depends so much about what you really want in your application. If you read mysqli_fetch_assoc will learn about this function is already returning an array, so call to iteration it's dispensable, principally with the order of array (vector, one dimension) is needed, because the function already do it for you, turning the seconde method the best option.

The first method is likable if you want to order your array more specifically, multidimensional kind of stuff or turn into an object.

PS: It's a good practice if your application it's fully OOP implemented, to do first method putting that information into a private var inside class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.