DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

Check if result set is empty

This is a discussion on Check if result set is empty within the PHP Development forums, part of the Programming & Scripting category; hi everyone starting to get into PHP, and I have trouble checking if a result set is empty. I am ...

Go Back   DeveloperBarn Forums > Programming & Scripting > PHP Development

  #1  
Old June 25th, 2008, 07:29 AM
Barn Regular
 
Join Date: Apr 2008
Posts: 84
Rep Power: 2
noFriends is on a distinguished road
Default Check if result set is empty

hi everyone

starting to get into PHP, and I have trouble checking if a result set is empty.

I am doing it like this:
Code:
$result = mysql_query($query);

if (mysql_fetch_array($result, MYSQL_ASSOC) == False) print "no records found";


	while($row = mysql_fetch_array($result, MYSQL_ASSOC))  //carry on looping through while there are records
	{
but this way, if there is a record, it will fetch it into the array, and my
while loop will skip the first record.

this is my first day with php, so be gentle
Reply With Quote
  #2  
Old June 25th, 2008, 07:34 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 292
Rep Power: 2
Shem is on a distinguished road
Default

you can try:

Code:
If (!$result) {
...code...
}
Else {
...code...
}
Or

here's an example of some of my code:
Code:
//get results of content query
	Public Function ResultsContent($GetAll) {
		//open conn to db
		$this->OpenDB();
			
			//get results
			$result = mysql_query($GetAll);
			$num_rows = mysql_num_rows($result);
			If($num_rows == 0) {
				$this->arrImage[] = array
				(
					'Data'=>array
					(
						'ID'=>0,
					)
				);
			}
			Else {
				while ($row = mysql_fetch_object($result)) {
					$this->arrContent[] = array
					(
						'Data'=>array
						(
							'ID'=>$row->idkey,
							'Content'=>$row->content,
							'Tags'=>$row->tags,
							'Timekey'=>$row->timekey,
							'Title'=>$row->title,
							'Type'=>$row->type,
							'Date'=>$row->date
						)
					);
				}
			}
		//close conn to db
		$this->CloseDB();
	}
where i just set ID=0 you can put: echo "nothing"

hope that helps some
Shem

Comments on this post
noFriends agrees: dankie
Reply With Quote
The Following User Says Thank You to Shem For This Useful Post:
noFriends (June 25th, 2008)
  #3  
Old June 25th, 2008, 07:35 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

Hey NF,

I'm not well experienced with PHP, either, but I believe you could do something like this:

Code:
$returned_rows = mysql_num_rows ($Query);
Then, do this:

Code:
if ($returned_rows == 0){
    echo 'No records found';
}else{
    echo 'There were ' . $returned_rows . 'records found.';
}
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
The Following User Says Thank You to jmurrayhead For This Useful Post:
noFriends (June 25th, 2008)
  #4  
Old June 25th, 2008, 07:39 AM
Barn Regular
 
Join Date: Apr 2008
Posts: 84
Rep Power: 2
noFriends is on a distinguished road
Default

thanks guys, the num_rows works 100%
Reply With Quote
  #5  
Old June 25th, 2008, 07:42 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 292
Rep Power: 2
Shem is on a distinguished road
Default

Quote:
Originally Posted by noFriends View Post
thanks guys, the num_rows works 100%
Did you try (!$result)?
I think that should work too

Shem
Reply With Quote
  #6  
Old June 25th, 2008, 07:47 AM
Barn Regular
 
Join Date: Apr 2008
Posts: 84
Rep Power: 2
noFriends is on a distinguished road
Default

the !result function is used to determine if your sql itself is flawed AFAIK.

tried it earlier, didn't work
Reply With Quote
  #7  
Old June 27th, 2008, 08:00 PM
don94403's Avatar
Moderator
 
Join Date: Mar 2008
Location: San Mateo, CA, USA
Posts: 146
Blog Entries: 8
Rep Power: 4
don94403 has a spectacular aura aboutdon94403 has a spectacular aura aboutdon94403 has a spectacular aura about
Default

That's right. There's a difference between a query that returns zero rows (which does NOT return a False result, just an empty result set) and a False result (which happens when there's a connection error or a syntax error). The PHP function mysql_num_rows($result) is what you want. It will return 0 or some positive integer.
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > PHP Development

Bookmarks

Thread Tools
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:48 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2