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? Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/ 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); Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/#findComment-754692 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); } } Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/#findComment-754694 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']; } } Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/#findComment-754702 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? Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/#findComment-754703 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. Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/#findComment-754704 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. Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/#findComment-754705 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. Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/#findComment-754706 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? Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/#findComment-754707 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 Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/#findComment-754709 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! Link to comment https://forums.phpfreaks.com/topic/143831-solved-best-pratice-for-ending-queries/#findComment-754710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.