Jump to content

Qusetion?


Trium918

Recommended Posts

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

}
?>

Link to comment
https://forums.phpfreaks.com/topic/53351-qusetion/
Share on other sites

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();
  }

?>

Link to comment
https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263677
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263691
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263723
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/53351-qusetion/#findComment-263726
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.