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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.