Results 1 to 6 of 6

Thread: What does mysql_num_rows return if no rows in the select set

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    What does mysql_num_rows return if no rows in the select set

    Trying to test that a record doesn't exist on a table and am unable to work out how to code around a record set with zero rows It doesn't return 0, which sucks somewhat.

    Anyone have any ideas

    Code:
    $q = "SELECT * FROM lb_group WHERE grp_id = '".$groupid."'";
    			
    $checkgroup = mysql_query($q, $connect);
    
    $num = mysql_num_rows($checkgroup):
    
    if(!$num) ??????????????

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: What does mysql_num_rows return if no rows in the select set

    I don't know whether to assume that you're getting a return value of -1, or false. if -1:
    PHP Code:
    if($num <= 0){
      
    // error?
    }else{
      
    // do something productive

    if false, you can just check if the variable evaluates to false, as you're doing (!$num). this will catch a value of false and a value of 0.

    mysql_num_rows() could maybe return -1, but will only return false if the function call failed for some reason.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Re: What does mysql_num_rows return if no rows in the select set

    Thanks Kows,

    Weirdly it returns a value if a row is found but isn't entirely communiative if one isn't.

    Seems to be working with some tweaking, but need to test a few more things before signing off on it. Guess I was using another language's constructs that don't work terribly well with PHP/MySQL.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: What does mysql_num_rows return if no rows in the select set

    PHP Code:
    if (is_resource($checkgroup)) 

  5. #5
    Member
    Join Date
    May 2010
    Posts
    58

    Re: What does mysql_num_rows return if no rows in the select set

    Try to get know even the query is done correctly or no first:

    Code:
    $q = "SELECT * FROM lb_group WHERE grp_id = '".$groupid."'";
    $checkgroup = mysql_query($q, $connect);
    if ($checkgroup == NULL)
    {
       //ERROR
    }
    else
    {			
      $num = mysql_num_rows($checkgroup):
    }

    and to make sure no error shows up then try:


    Code:
    if( ($num == 0) OR ($num == NULL) )   {
      // error?
    }else{
      // do something productive
    }
    I used this and it was working fine for me in my projects.
    Last edited by Luinox86; May 30th, 2010 at 08:17 PM.

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: What does mysql_num_rows return if no rows in the select set

    mysql_query() and mysql_num_rows() do not return null. they either return a resource/integer (respectively) or boolean false on error.

Posting Permissions

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



Click Here to Expand Forum to Full Width