+ Reply to Thread
Results 1 to 7 of 7

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
    104
    Rep Power
    3

    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
    303
    Rep Power
    3

    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
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    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 my post helped you, click "Thanks".
    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
    104
    Rep Power
    3

    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
    303
    Rep Power
    3

    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
    104
    Rep Power
    3

    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
    172
    Blog Entries
    8
    Rep Power
    5

    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 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