Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

Howto check for empty results
Welcome, Guest. Please login or register.
November 23, 2008, 09:06:25 AM
11307 Posts in 1250 Topics by 501 Members
Latest Member: rosaline
Experts Round Table Network  |  Databases  |  MySQL  |  Howto check for empty results « previous next »
Pages: [1]
Author Topic: Howto check for empty results  (Read 679 times)
thepreacher

Offline Offline

Posts: 77


« on: November 24, 2007, 03:57:13 PM »

The results of a select statement could be empty - ie the is no data in the table matching the search string.

$result = mysql_query($query); - ie results is empty.

What will $ results contain when that happens. How do i check for empty results.

Thanks
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #1 on: November 24, 2007, 08:29:30 PM »

False.

e.g.


       $result = mysql_query($query); - ie results is empty.
       if (!$result) {
      $message  = 'Invalid query: ' . mysql_error() . "\n";
      die($message);
   }
Logged

Rod
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 489



WWW
« Reply #2 on: November 25, 2007, 01:56:09 AM »

Its been awhile, but doesn't mysql_query() only return false when the query failed as in syntax error or such, and returns true when query succeeded. So if a query succeeded in being executed it will return true even if the result was no rows, or 1 or more rows. Thats what I always have assumed from the return values for mysql_query() and that the above method is used for debugging and mysql_num_rows() is used for checking if a row was returned or not.

Code
Language: php (GeSHi-highlighted)
<?php
 
$result = mysql_query($query) or die(mysql_error());
if (0 < mysql_num_rows($result))
{
   // 1 or more rows returned
}
else
{
   // no rows returned
}
 
?>
Logged

[x] Fight | www.crypticmauler.com
"You must be
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #3 on: November 26, 2007, 10:24:21 AM »

yes. Rod is wrong this time ;-)

mysql_query returns false only in case of failure
in case you want to check if there were no results, mysql_num_rows() is your friend. It can ***only*** be used when the returned value of mysql_query() was TRUE, oeuf corse.

so again ther structure to use is the one explained in the online manual, ie

Code:
$linkid=mysql_connect(); // also select db etc...
$query="...";
$result=mysql_query($query,$linkid); // or die("failed on '$query' with error = ".mysql_error());
if ($result!==FALSE) { // query ok
  if (($a=mysql_num_rows($result))>0) { // dataset contains at least oen row returned
    // echo "$a rows returnd"; // now is time to use : while ($res=mysql_fetch_array($result)) { etc }
  } else { // no rows returned
    // echo "no rows returned";
  }
} else { // query failed
  // die("query failed"); ?
}

not checked against typos.
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
Pages: [1]
« previous next »
    Jump to: