By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

How to determine if 0 rows were returned from query

Claus Mygind
100+
P: 571
I use the following construct to query my MySQL database tables. Instead of using the flag $recFound, I would like to just examine and determine if the associative array in $result is empty.

I know that the "while" loop will not execute if $result is empty, but that is not enough for me. I would like to test the difference between an invalid query and a successful query with zero rows returned

Expand|Select|Wrap|Line Numbers
  1. if ($result = $db->query($sql)){
  2.  
  3.         $recFound = false;
  4.         while ($c = $result->fetch_assoc()) {
  5. ..... some processing steps here.....
  6.  
  7.                 $recFound = true;
  8.         }
  9.  
  10.         if ($recFound){
  11.             $cResponse = subStr( $cResponse, 0, strlen($cResponse)-3) )
  12.         }else{
  13.             $cResponse = "none";
  14.         }
  15.     }
  16.  
Can this code be used, if so how would it be re-written to fit my code.

Expand|Select|Wrap|Line Numbers
  1. if (mysql_num_rows($result) == 0) {
  2.     echo "No rows found, nothing to print so am exiting";
  3.     exit;
  4. }
Mar 26 '12 #1

✓ answered by Claus Mygind

Thank you very much for turning me in the right direction. I think the associative array aspect of $result is what confused me. From what you provided me I found this in the online php manual.

Expand|Select|Wrap|Line Numbers
  1. /* determine number of rows result set */
  2.     $row_cnt = $result->num_rows;
  3.  

Share this Question
Share on Google+
4 Replies


Dormilich
Expert Mod 5K+
P: 8,639
a) donít use the outdated mysql extension.
b) you can use the SQL function COUNT() for that
c) rewriting that code depends on your actual database code, of which I have not enough information to give any sensible advice
Mar 27 '12 #2

Claus Mygind
100+
P: 571
Actually it is more of a php question than mysql. Also while I am not using PDO I am using mysqli.

The example I gave from the online help is only an example of code that should be available. I do not believe it has been deprecated.

I define my connection code in an object called db, the query result is then stored in $results. $result then contains all rows returned from the mysqli->query.

So then is it reasonable to expect there is a mysqli_num_rows equivelent. Also if db is a reference to the mysqli connection, is there something like db_num_rows.

here is how I define the reference to mysqli
Expand|Select|Wrap|Line Numbers
  1. class dbcnx extends mysqli
  2. {
  3. .... functions and methods
  4. }
  5.  
  6.     $db = new dbcnx(my connection info);
  7.  
  8.  
  9.  
From this additional info, is there a way using php to determine if zero rows have been returned?
Mar 28 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
first your question:
I would like to just examine and determine if the associative array in $result is empty.
from MySQLi_Result->fetch_assoc():
Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset.

I think that is unambiguous …

From this additional info, is there a way using php to determine if zero rows have been returned?
MySQLi_Result->num_rows
Mar 28 '12 #4

Claus Mygind
100+
P: 571
Thank you very much for turning me in the right direction. I think the associative array aspect of $result is what confused me. From what you provided me I found this in the online php manual.

Expand|Select|Wrap|Line Numbers
  1. /* determine number of rows result set */
  2.     $row_cnt = $result->num_rows;
  3.  
Mar 28 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.