Trium918 Posted May 29, 2007 Share Posted May 29, 2007 Which is the best way to do this? Like this <?php $sql = mysql_query("SELECT * FROM table")or die(mysql_error()); $num_results = mysql_num_rows($sql); for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($sql); //result } ?> or <?php $sql = "SELECT * FROM table"; $res =mysql_query($sql) or die (mysql_error()); $num_results = mysql_num_rows($res); for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($res); //result } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53351-qusetion/ Share on other sites More sharing options...
dustinnoe Posted May 29, 2007 Share Posted May 29, 2007 I like the second option because most of my queries are rather long so the code just looks cleaner. In the case of a short UPDATE or DELETE query I will go with something similar to the first option. Quote Link to comment https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263672 Share on other sites More sharing options...
rameshfaj Posted May 29, 2007 Share Posted May 29, 2007 From optimization point of view the former is better! Quote Link to comment https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263674 Share on other sites More sharing options...
trq Posted May 29, 2007 Share Posted May 29, 2007 Neither is very good. <?php // connect $sql = "SELECT foo FROM tbl"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo $row['foo']."<br />"; } } else { echo "No results found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263677 Share on other sites More sharing options...
gabeg Posted May 29, 2007 Share Posted May 29, 2007 Neither is very good. <?php // connect $sql = "SELECT foo FROM tbl"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo $row['foo']."<br />"; } } else { echo "No results found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } ?> This is gets my vote. Similar as to what I was going to suggest Quote Link to comment https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263691 Share on other sites More sharing options...
dustinnoe Posted May 29, 2007 Share Posted May 29, 2007 I thought we were just voting on setting the query into it's own variable as opposed to setting the result of mysql_query() in $sql. I do like thorpe's submission as an overall solution though. Quote Link to comment https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263695 Share on other sites More sharing options...
Barand Posted May 29, 2007 Share Posted May 29, 2007 The expensive elements are time spent developing/debugging and maintaining code, not shaving a couple of millisecs off the execution time. So I go with the increased readability and debugging from putting it in a separate variable. My 0.02. Quote Link to comment https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263723 Share on other sites More sharing options...
dustinnoe Posted May 29, 2007 Share Posted May 29, 2007 The expensive elements are time spent developing/debugging and maintaining code, not shaving a couple of millisecs off the execution time. So I go with the increased readability and debugging from putting it in a separate variable. My 0.02. What if strlen($sql) < 25ish? would you go for saving a few milliseconds then? Quote Link to comment https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263726 Share on other sites More sharing options...
Barand Posted May 29, 2007 Share Posted May 29, 2007 If the sql contains variables, yes, for debugging. Although, I admit I do $res = mysql_query("SELECT COUNT(*) FROM table"); but then there isn't much to debug. Quote Link to comment https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-264096 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.