DamienRoche Posted January 2, 2009 Share Posted January 2, 2009 I am using a while loop to run through rows and extract the data. The data is being presented and fancied up using html. My question is: would it be more efficient to use the while loop or put the data into an array and use a for each on the array? NOTE: there is only 1 row below for example purposes - assume all works, there may be some typos. Actual version works and has over 1000 cols/variables with up to 10,000 rows of data. Method 1: while($fetch=mysql_fetch_array($data)){ $t = mysql_real_escape_string($fetch['title']); echo "<h1>$t</h1>"; } Method 2: while($fetch=mysql_fetch_array($data)){ $t[] = mysql_real_escape_string($fetch['title']); } foreach($t as $t2){ echo "<h1>$t</h1>"; } ....no doubt there are other ways, but which do you think is more efficient. Thanks. Link to comment https://forums.phpfreaks.com/topic/139245-question-on-phpmysql-efficiency-which-method/ Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 It's always better to run as few queries as possible. So first example. And why do you run mysql_real_escape_string on this data? Link to comment https://forums.phpfreaks.com/topic/139245-question-on-phpmysql-efficiency-which-method/#findComment-728360 Share on other sites More sharing options...
DamienRoche Posted January 2, 2009 Author Share Posted January 2, 2009 Thanks, and I don't know why I run mysql_real_escape_string on this....I seen somebody else use it on the output, so I copied their logic. I just thought "the more, the better". So I don't need it for output? just for user input? Thanks again. Link to comment https://forums.phpfreaks.com/topic/139245-question-on-phpmysql-efficiency-which-method/#findComment-728365 Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 mysql_real_escape_string is meant to protect database from malicious inputs. Assuming you're protecting your database right, you shouldn't have to do any escaping when outputting data. Link to comment https://forums.phpfreaks.com/topic/139245-question-on-phpmysql-efficiency-which-method/#findComment-728369 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.