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
https://forums.phpfreaks.com/topic/167244-mysql_fetch_assoc-vs-foreach/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.