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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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'];
  }
}

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
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.