Jump to content

[SOLVED] Best Pratice For Ending Queries


limitphp

Recommended Posts

If all I wanted to do was something as simple as getting an artistName from a table what is the best practice for that as far as ending the query?

 

Here's what I've been doing:

$queryArtistName = "SELECT artistName FROM artists WHERE artistID = '$artistID'";
$resultArtistName = mysql_query($queryArtistName) or die (mysql_error());
$rowArtistName = mysql_fetch_assoc($resultArtistName);
$artistName = $rowArtistName['artistName'];

 

If I'm done with that query, is there something I should do at the end?  Or is that fine to leave it open like that?

 

Link to comment
https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/
Share on other sites

Adding mysql_free_result PHP doesn't require it (unless you use strict PHP), but it helps your server...

$queryArtistName = "SELECT artistName FROM artists WHERE artistID = '$artistID'";
$resultArtistName = mysql_query($queryArtistName) or die (mysql_error());
$rowArtistName = mysql_fetch_assoc($resultArtistName);
$artistName = $rowArtistName['artistName'];
mysql_free_result($resultArtistName);

If I'm done with that query, is there something I should do at the end?  Or is that fine to leave it open like that?

 

Its fine to leave your connection open, and really no need to free the result for such a small query, php's garbage collection will take care of it.

 

Your code could be improved however.

 

$sql = "SELECT artistName FROM artists WHERE artistID = '$artistID' LIMIT 1";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $artistName = mysql_result($result, 0);
  }
}

$sql = "SELECT artistName FROM artists WHERE artistID = '$artistID' LIMIT 1";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $artistName = mysql_result($result, 0);
  }
}

 

I'm not too fond of that method, I much prefer this method:

 

$sql = "SELECT artistName FROM artists WHERE artistID = '$artistID' LIMIT 1";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_array($result);
    $artistName = $row['artistName'];
  }
}

If I'm done with that query, is there something I should do at the end?  Or is that fine to leave it open like that?

 

Its fine to leave your connection open, and really no need to free the result for such a small query, php's garbage collection will take care of it.

 

Your code could be improved however.

 

$sql = "SELECT artistName FROM artists WHERE artistID = '$artistID' LIMIT 1";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $artistName = mysql_result($result, 0);
  }
}

If the first if hits (if ($result = mysql_query($sql)), doesn't that mean it got results?

 

 

I'm not too fond of that method, I much prefer this method:

$sql = "SELECT artistName FROM artists WHERE artistID = '$artistID' LIMIT 1";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_array($result);
    $artistName = $row['artistName'];
  }
}

 

Me too normally. But for a single field and a single row, this is probably the most efficient.

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.