ainoy31 Posted July 5, 2007 Share Posted July 5, 2007 What is the best way to check for a result set that is empty? I have the following: $query = "SELECT * FROM nHeal WHERE order_id='$order_id'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); if(empty($row)) { header("Location: https://xxx.com/nHeal_ainoy/orderform.php"); } else { ......etc } it does not redirect as i wanted it. thx. Link to comment https://forums.phpfreaks.com/topic/58559-checking-an-empty-result/ Share on other sites More sharing options...
soycharliente Posted July 5, 2007 Share Posted July 5, 2007 Are you trying to check if a field is empty or if it returned 0 rows? You need to put a field after $row ($row["yourfield"]) or check how many rows were returned then redirect. Link to comment https://forums.phpfreaks.com/topic/58559-checking-an-empty-result/#findComment-290490 Share on other sites More sharing options...
ainoy31 Posted July 5, 2007 Author Share Posted July 5, 2007 I am trying to check if it returns 0 rows. I guess the empty() function does not handle it Link to comment https://forums.phpfreaks.com/topic/58559-checking-an-empty-result/#findComment-290501 Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 <?php $query = "SELECT * FROM nHeal WHERE order_id='$order_id'"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { $row = mysql_fetch_array($result); // etc } else { header("Location: https://xxx.com/nHeal_ainoy/orderform.php"); } } else { echo mysql_error(); } ?> Link to comment https://forums.phpfreaks.com/topic/58559-checking-an-empty-result/#findComment-290503 Share on other sites More sharing options...
trq Posted July 5, 2007 Share Posted July 5, 2007 I am trying to check if it returns 0 rows. I guess the empty() function does not handle it Your problem is that mysql_query returns true / false depending on query success / failure, not the amount of rows returned. You need to use mysql_num_rows to see how many rows where returned by a sucessfull query. Link to comment https://forums.phpfreaks.com/topic/58559-checking-an-empty-result/#findComment-290504 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.