+ Reply to Thread
Results 1 to 10 of 10

Thread: Check if result set is empty

  1. #1
    Barn Frequenter noFriends is on a distinguished road noFriends's Avatar
    Join Date
    Apr 2008
    Posts
    176
    Rep Power
    4

    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

  2. #2
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    305
    Rep Power
    4

    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

  3. #3
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,533
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    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, give me rep.
    If you like it here...throw us a few bones to help support us.


  4. #4
    Barn Frequenter noFriends is on a distinguished road noFriends's Avatar
    Join Date
    Apr 2008
    Posts
    176
    Rep Power
    4

    thanks guys, the num_rows works 100%

  5. #5
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    305
    Rep Power
    4

    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

  6. #6
    Barn Frequenter noFriends is on a distinguished road noFriends's Avatar
    Join Date
    Apr 2008
    Posts
    176
    Rep Power
    4

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

    tried it earlier, didn't work

  7. #7
    Moderator don94403 is a jewel in the rough don94403 is a jewel in the rough don94403 is a jewel in the rough don94403's Avatar
    Join Date
    Mar 2008
    Location
    San Mateo, CA, USA
    Posts
    313
    Blog Entries
    8
    Real Name
    Don Ravey
    Rep Power
    6

    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.

  8. #8
    Barn Newbie nikoda is an unknown quantity at this point nikoda's Avatar
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    2

    Quote Originally Posted by jmurrayhead View Post
    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.';
    }
    

    Became a member just to tell you that this was a great solution that I absolutely love.

    What a great guy! Thanks man!

  9. #9
    Barn Newbie Miss Tweaker is an unknown quantity at this point Miss Tweaker's Avatar
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    1

    Quote Originally Posted by jmurrayhead View Post
    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.';
    }
    

    Brilliant! Have been searching the entire morning for this answer.

    THANKS!

  10. #10
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,533
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Quote Originally Posted by nikoda View Post
    Became a member just to tell you that this was a great solution that I absolutely love.

    What a great guy! Thanks man!
    Quote Originally Posted by Miss Tweaker View Post
    Brilliant! Have been searching the entire morning for this answer.

    THANKS!
    You're welcome
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO