Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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