asmith Posted July 24, 2009 Share Posted July 24, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167244-mysql_fetch_assoc-vs-foreach/ Share on other sites More sharing options...
dzelenika Posted July 24, 2009 Share Posted July 24, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/167244-mysql_fetch_assoc-vs-foreach/#findComment-881866 Share on other sites More sharing options...
asmith Posted July 24, 2009 Author Share Posted July 24, 2009 Actually it is in pagination, showing about 20 rows per page, In each row about 15 fields are being read. Quote Link to comment https://forums.phpfreaks.com/topic/167244-mysql_fetch_assoc-vs-foreach/#findComment-881910 Share on other sites More sharing options...
rhodesa Posted July 24, 2009 Share Posted July 24, 2009 i can't imagine why, for any reason, the second case would be faster. i can see the need for it in certain cases though...wherever you need to know future date before outputting the first row Quote Link to comment https://forums.phpfreaks.com/topic/167244-mysql_fetch_assoc-vs-foreach/#findComment-881914 Share on other sites More sharing options...
waynew Posted July 24, 2009 Share Posted July 24, 2009 The second example is pretty redundant. The first one prints the data straight out whereas the second example assigns the values from each row to an array and the starts ANOTHER loop just to print it out. Quote Link to comment https://forums.phpfreaks.com/topic/167244-mysql_fetch_assoc-vs-foreach/#findComment-882333 Share on other sites More sharing options...
Daniel0 Posted July 25, 2009 Share Posted July 25, 2009 The second will need more memory. If you need to pass an entire result set to something else you can lazy load it for improved performance. I once wrote an example of that. Try to search the forums for it. Quote Link to comment https://forums.phpfreaks.com/topic/167244-mysql_fetch_assoc-vs-foreach/#findComment-882518 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.