I'm looking for the best way to check and see if any results were returned in a query. I feel like I write this part of code a lot and sometimes I get errors, and sometimes I don't.
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 I do it:
if (!$result) { PERFORM ACTION }
If the first way doesn't work, then sometimes this will:
if (mysql_num_rows($result)==0) { PERFORM ACTION }
Then I even saw that I could do it this way the other day:
list($total) = mysql_fetch_row($result);
if ($total==0) { PERFORM ACTION }
What is the best way to do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…