c_pattle Posted October 14, 2010 Share Posted October 14, 2010 I have this code which performs a mysql query. However is there a way I can check to see if the set it returns is empty. If it is empty I will then change the sql statement. Thanks $announcement_sql = "select * where annnouncement_for like \"%" . $_POST['for_search'] . "%\" and announcement_verification=\"1\" order by announcement_number desc limit 0, 15"; Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 14, 2010 Share Posted October 14, 2010 $result = mysql_quey($announcement_sql); if(mysql_num_rows($result) === 0) { // no rows were returned } Quote Link to comment Share on other sites More sharing options...
c_pattle Posted October 14, 2010 Author Share Posted October 14, 2010 Thanks I have tried the following code but I always get this error message - "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/jon/index.php on line 115". if (isset($_POST['for_submit'])) { $announcement_sql = "select * where annnouncement_for like \"%" . $_POST['for_search'] . "%\" and announcement_verification=\"1\" order by announcement_number desc limit 0, 15"; $num_rows = mysql_num_rows(mysql_query($announcement_sql, $mysql_conn)); if (!$num_rows) { $announcement_sql = "select * from announcements where announcement_verification=\"1\" order by announcement_number desc limit 0, 15"; } Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 14, 2010 Share Posted October 14, 2010 That means that there was an error in your query. You should check for that as well. Something like: $result = mysql_query($announcement_sql) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
Pawn Posted October 14, 2010 Share Posted October 14, 2010 Aways check the result of mysql_query. The more verbose your error handling, the easier you will find it to debug your code. $sql = "SELECT col FROM tbl_name"; if(!$query = mysql_query($sql)) { echo "An error occured on line ".__LINE__.".<br />\n" . "MySQL said: ".mysql_error().".<br /><br />\n" . "Query: ".$sql; exit; } $num_rows = mysql_num_rows($query); if($num_rows == 0) { echo "No results were returned.<br /><br />\n" . "Query: ".$sql; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.