Jump to content

mysql_fetch_assoc vs foreach


asmith

Recommended Posts

I've been getting my tabular data from database and printing it on the screen like this :

 

<?php

while ($row = mysql_fetch_assoc($result))
     echo '<tr><td>...';

?>

 

I've noticed in some scripts that the writer have done something like this:

 

<?php

$content = array();
while ($row = mysql_fetch_assoc($result))
    $content[$row['field']] = $value;

foreach ($content as $field => $value)
     echo '<tr><td>...';

?>

 

They get the data and store it in an array, Then use the loop for an array.

At first I thought "well storing in some place else, Then again using a loop for it? That's makes yoru script slower"

 

But when I actually tried the script, It seemed that it is working even faster than mine (The direct echo from looping mysql result)

 

Am I missing something here, Or storing the data in an array then go through the loop is faster? 

 

 

 

Link to comment
Share on other sites

What amount of data you have to see the difference?

 

It is possible that first script is slower because it is wandering between database reading and writing HTTP response, while second in first step only reads data from database and in second writes it...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.