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
https://forums.phpfreaks.com/topic/212163-best-way-to-identify-empty-set/
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

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

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.