limitphp Posted February 4, 2009 Share Posted February 4, 2009 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? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 4, 2009 Share Posted February 4, 2009 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); Quote Link to comment Share on other sites More sharing options...
trq Posted February 4, 2009 Share Posted February 4, 2009 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); } } Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 4, 2009 Share Posted February 4, 2009 $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']; } } Quote Link to comment Share on other sites More sharing options...
limitphp Posted February 4, 2009 Author Share Posted February 4, 2009 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? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 4, 2009 Share Posted February 4, 2009 if ($result = mysql_query($sql) That just says the query was successful if it goes into that if statement. Quote Link to comment Share on other sites More sharing options...
trq Posted February 4, 2009 Share Posted February 4, 2009 If the first if hits (if ($result = mysql_query($sql)), doesn't that mean it got results? No. It only means your query worked. Quote Link to comment Share on other sites More sharing options...
trq Posted February 4, 2009 Share Posted February 4, 2009 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. Quote Link to comment Share on other sites More sharing options...
limitphp Posted February 4, 2009 Author Share Posted February 4, 2009 If the first if hits (if ($result = mysql_query($sql)), doesn't that mean it got results? No. It only means your query worked. like it didn't have any errors in it? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 4, 2009 Share Posted February 4, 2009 If the first if hits (if ($result = mysql_query($sql)), doesn't that mean it got results? No. It only means your query worked. like it didn't have any errors in it? Right Quote Link to comment Share on other sites More sharing options...
limitphp Posted February 4, 2009 Author Share Posted February 4, 2009 If the first if hits (if ($result = mysql_query($sql)), doesn't that mean it got results? No. It only means your query worked. like it didn't have any errors in it? Right thanks guys! Quote Link to comment 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.