Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 15th, 2012, 02:08 PM
Registered User
 
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?
 
Old February 16th, 2012, 08:08 AM
Friend of Wrox
 
Join Date: May 2011
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
}
 
Old February 17th, 2012, 08:32 AM
Registered User
 
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
 
Old February 18th, 2012, 07:27 AM
Friend of Wrox
 
Join Date: May 2011
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..
The Following User Says Thank You to UseLess For This Useful Post:
gkman (February 18th, 2012)





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.