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"; Link to comment https://forums.phpfreaks.com/topic/215875-check-is-sql-statement-is-valid/ 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 } Link to comment https://forums.phpfreaks.com/topic/215875-check-is-sql-statement-is-valid/#findComment-1122204 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"; } Link to comment https://forums.phpfreaks.com/topic/215875-check-is-sql-statement-is-valid/#findComment-1122211 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()); Link to comment https://forums.phpfreaks.com/topic/215875-check-is-sql-statement-is-valid/#findComment-1122217 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; } Link to comment https://forums.phpfreaks.com/topic/215875-check-is-sql-statement-is-valid/#findComment-1122220 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.