Jump to content

Best way to identify empty set


c_pattle

Recommended Posts

I was just wondering what the best way to identify an empty set is.  At the moment I have this but this doesn't seem to work so I was wondering what the best way is.  Thanks. 

 

$article_rs = mysql_query($article_sql, $mysql_conn);

if (!$article_rs) {
$_SESSION['no_articles'] = "<p>Sorry but there are no articles that match your search.</p>";
}

Link to comment
Share on other sites

if(!empty($article_rs)) {

 

http://php.net/manual/en/function.empty.php

 

Also mysql_query is a resource, and it will not be empty just because there are no results. You would need to use one of the mysql functions to actually retrieve the results or count the result rows in order to store that information (or lack thereof) in a variable. See:

 

http://www.php.net/manual/en/function.mysql-num-rows.php

Link to comment
Share on other sites

Use the function mysql_num_rows. This function returns how many results was returned from your query. So your code will be

 

$article_rs = mysql_query($article_sql, $mysql_conn);

// check that mysql_query ran fine
// mysql_query will return false if there is an error
if ($article_rs)
{
    // check if any results where returned
    if(mysql_num_rows($article_rs))
    {
        // process the query results here
    }
    // no results, set the error
    else
    {
$_SESSION['no_articles'] = "<p>Sorry but there are no articles that match your search.</p>";
    }
}
// Problem with mysql_query! Lets see why
else
{
    trigger_error('MySQL Error: ' . mysql_error() . '<br />Query: ' . $article_sql, E_USER_ERROR);
}

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.