Jump to content

check is sql statement is valid


c_pattle

Recommended Posts

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

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";
}

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.