c_pattle Posted August 31, 2010 Share Posted August 31, 2010 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/212163-best-way-to-identify-empty-set/ Share on other sites More sharing options...
jayarsee Posted August 31, 2010 Share Posted August 31, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212163-best-way-to-identify-empty-set/#findComment-1105556 Share on other sites More sharing options...
wildteen88 Posted August 31, 2010 Share Posted August 31, 2010 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); } Quote Link to comment https://forums.phpfreaks.com/topic/212163-best-way-to-identify-empty-set/#findComment-1105557 Share on other sites More sharing options...
c_pattle Posted August 31, 2010 Author Share Posted August 31, 2010 Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/212163-best-way-to-identify-empty-set/#findComment-1105558 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.