I am looking for the best way to check and see if any results were returned in the query. It seems to me that I often write this part of the code, and sometimes I get errors, and sometimes not.
For example, I run this query to check if a username exists before inserting a new one into the database.
$result = mysql_query("SELECT * FROM ...");
Then I want to check and see if any results were returned. Here is one way to do this:
if (!$result) { PERFORM ACTION }
If the first method does not work, sometimes it will be:
if (mysql_num_rows($result)==0) { PERFORM ACTION }
Then I even saw that I could do it as follows:
list($total) = mysql_fetch_row($result); if ($total==0) { PERFORM ACTION }
What is the best way to do this?
php mysql
timroman Nov 26 '10 at 15:37 2010-11-26 15:37
source share