Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143
This is the forum to discuss the Wrox book Beginning PHP 6, Apache, MySQL 6 Web Development by Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz; ISBN: 9780470391143
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 15th, 2012, 02:08 PM
Registered User
Points: 32, Level: 1
Points: 32, Level: 1 Points: 32, Level: 1 Points: 32, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default how do i check if query is empty?

Hey everyone.

I am trying to check if my query returned any row of info.

I tried using the empty() function - fail.
I guess this is because my $result isnt a empty variable- it contains an empty query.

I tried to use the mysql_num_rows()==0 function - also no good.
This is because it is possible that my query will return only one row of info (and it does) which will cause the above function to return 0.

I did try:

Code:
$result = mysql_fetch_array($result)
if ( mysql_num_rows($result)==0 && $result[0] = ' ')
That seemed to worke but it doesn't seem right to me because it is possible to have an empty block of info, And this condition won't be good for that.

So how do you check if the query is empty?
Reply With Quote
  #2 (permalink)  
Old February 16th, 2012, 08:08 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Code:
$sql = 'SELECT * FROM table';
if( !$result = mysql_query($sql) )
{
	// $result is false and this indicates
	// the query failed so output a message
	// optionally stop processing script here
}
else
{
	// query was successful
	// so start a loop to grab each row from the database
	// the loop will end when there are no more rows to be returned
	// in this case $row will be false
	while( $row = mysql_fetch_array($result);
	{
		// do something with the data
	}
}
Another way to do the above but this time testing to see if anything is returned

Code:
$sql = 'SELECT * FROM table';
if( !$result = mysql_query($sql) )
{
	// $result is false and this indicates
	// the query failed so output a message
	// optionally stop processing script here
}

// test to see if anything was returned by the query
if( $row = mysql_fetch_array($result) )
{
	// db returned something so process it
	do
	{
		// process data
		// data will be in the $row variable used above
		
		// do while loops run once as the test is done below to determine
		// if the loop should actually loop
	}
	while( $row = mysql_fetch_array($result) );
}
else
{
	// optional
	// display message that nothing was returned from db
}
Reply With Quote
  #3 (permalink)  
Old February 17th, 2012, 08:32 AM
Registered User
Points: 32, Level: 1
Points: 32, Level: 1 Points: 32, Level: 1 Points: 32, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I just tried Both of your suggestion- none of them worked.
I think I may have a problem with my installed php because suddenly the mysql_num_rows() function does the trick and the php stopped handling errors
Reply With Quote
  #4 (permalink)  
Old February 18th, 2012, 07:27 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

In your example above you're using this;
Code:
$result = mysql_fetch_array($result)
if ( mysql_num_rows($result)==0 && $result[0] = ' ')
So assuming you're putting the result of the query into $result you cannot then use;
Code:
$result = mysql_fetch_array($result);
as you have just destroyed the variable holding the db pointer to the query result. So if there is more than one row to be returned from the db you won't get them as you have just destroyed the only way to access them.

Therefore in this case the result of the 'mysql_num_rows' call could be meaningless due to destroying the valid db pointer and turning it into an array instead.

So basically get the result of the query into a variable and then each row from the db into another variable....

Last edited by UseLess; February 18th, 2012 at 07:35 AM..
Reply With Quote
The Following User Says Thank You to UseLess For This Useful Post:
gkman (February 18th, 2012)
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recordset Empty Check Coby Access VBA 2 April 27th, 2007 04:09 PM
best way to check for variable being empty crmpicco Classic ASP Basics 3 March 28th, 2006 12:11 PM
How to check if you have an empty array Ciarano VB How-To 9 March 31st, 2004 09:17 AM
check if empty hosefo81 Javascript How-To 12 March 9th, 2004 08:43 AM
How to check if collection is empty Ciarano Beginning VB 6 2 March 5th, 2004 08:11 AM



All times are GMT -4. The time now is 06:07 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.