473,396 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to determine if 0 rows were returned from query

Claus Mygind
571 512MB
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.  

4 2072
Dormilich
8,658 Expert Mod 8TB
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
571 512MB
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
8,658 Expert Mod 8TB
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
571 512MB
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

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

Similar topics

6
by: yoohanman | last post by:
Hi, I have a select statment that correctly returns zero rows at times. I would like to be able to return the value 0 (a single row with the value 0) whenever the logic returns zero rows. ...
7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
1
by: BF | last post by:
Hello, I am trying to find out if there is a way to limit the number of rows returned when a cursor is opened. I am using DB2 version 7 on z/OS. The SELECT statement in my DECLARE CURSOR...
2
by: AnnieLab | last post by:
In Oracle you can restrict the number of rows returned by specifying 'WHERE ROWNUM < 10'. Can you do this in DB2 as well?
3
by: Juan | last post by:
how to get number of rows returned by a SqlDataReader without having to scroll it?
4
by: Chris Tremblay | last post by:
I am trying to figure out how to go about retrieving the number of results returned from my queries in SQL server from VB.NET without using a the Select Count(*) query. The method that I was using...
0
by: John A Grandy | last post by:
In ASP.NET 2.0 , does the ObjectDataSource provide a method or property to return the number of rows returned ?
4
by: esmith2112 | last post by:
I have a situation that I can't explain. Boiled down to its essence, I have a query of the form SELECT A.COL1, A.COL2, B.COL1 FROM A LEFT JOIN B ON A.KEY = B.KEY This query produces 5383...
0
by: Lily12 | last post by:
Hi, I have writen a user defined function which returns a table.I want to add the table rows returned by UDF in the SQL statement which calls the function. Can anyone suggest me. Thanks in...
2
by: yuva17raj | last post by:
hi i am using the select query with group by, so that i am getting records with more than one column so that i need to calculate the number of rows returned by the query for example with my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.