Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does MySQL query return anything?

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
How can I test if a MySQL query has returned an empty array? This code always says something is returned even if when I then extract each row from $result there is no data in the array.
Code:
$result = mysql_query($query, $conn);
if (!empty($result)) echo "Query returned something";

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?
 
Take a look at the manual entry for mysql_query()

$result will never really be empty.

Mysql_query returns a resource when the query successfully completes, even if no actual rows are returned. It will return false in the event the query fails. 0 rows returned is not a failure it simply means no rows matched the query criteria. For that reason $result will never truly be empty, though being false can be considered to be the same as being empty in certain circumstances, your condition is not exactly correct.

If you need to know whether 0 rows were returned use mysql_num_rows to find the exact number of rows returned.

Code:
$result = mysql_query($query, $conn);
if ($result[red]!=[/red]false) echo "Query completed successfully. Whether or not it returned rows is unknown at this point";
if($result[red]==[/red]false){echo "Query did not complete successfully";}
if(mysql_num_rows($result) > 0 ){echo "Query returned at least one row"}; 
if(mysql_num_rows($result) == 0 ){echo "Query completed successfully but returned no rows"; }







----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top