How do I get the total number of rows returned by a MySQL query

Discussion in 'PHP' started by egdcltd, Jun 28, 2007.

  1. #1
    I want to know the total number of rows being returned by the following query

    				$sql = "SELECT *
    					FROM " . ADR_LIBRARY_TABLE ."
    					WHERE book_difficulty <= $book_difficulty 
    					AND book_zone = $area_id";
    				$result = $db->sql_query($sql);
    				if( !$result )
    				{
    					message_die(GENERAL_ERROR, 'Could not obtain book information', "", __LINE__, __FILE__, $sql);
    				}
    				$library = $db->sql_fetchrow($result);
    PHP:
    I thought that
    mysql_num_rows($library);
    PHP:
    would return this, but it causes an error
    mysql_num_rows(): supplied argument is not a valid MySQL result resource
    Code (markup):
     
    egdcltd, Jun 28, 2007 IP
  2. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Never mind, think I see what I was doing wrong. Should have used this instead

    mysql_num_rows($result);
    PHP:
     
    egdcltd, Jun 28, 2007 IP
  3. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Slightly different problem now
    
    				$sql = "SELECT *
    					FROM " . ADR_LIBRARY_TABLE ."
    					WHERE book_difficulty <= $book_difficulty 
    					AND book_zone = $area_id";
    				$result = $db->sql_query($sql);
    				if( !$result )
    				{
    					message_die(GENERAL_ERROR, 'Could not obtain book information', "", __LINE__, __FILE__, $sql);
    				}
    				$books = mysql_num_rows($result);
    				$library = $db->sql_fetchrow($result);
    			}
    
    			if ( !$library )
    			{
    				adr_previous ( Adr_library_failure , adr_research , '' );
    			}
    			else
    			{
    				// Now roll for research
    				$rnd_research = mt_rand ( 1 , $books);
    
    				$sql = "SELECT *
    					FROM " . ADR_LIBRARY_TABLE ."
    					WHERE book_id = '".$library[$rnd_research]['book_id']."' ";
    				$result = $db->sql_query($sql);
    				if( !$result )
    				{
    					message_die(GENERAL_ERROR, 'Could not obtain book information', "", __LINE__, __FILE__, $sql);
    				}
    				$research_result = $db->sql_fetchrow($result);
    PHP:
    The above code is supposed to get all possible results for a MySQL query and store them as $library. Then, a random result is called from this list, and all it's information retrieved from the database. The first query works, the second doesn't.
     
    egdcltd, Jun 28, 2007 IP
  4. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #4
    do you have any errors ? or script doesn't reach that ELSE?
    did you check $library value?
     
    gibex, Jun 28, 2007 IP
  5. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No errors. By echo statements, I've found that $library[$rnd_research]['book_id'] has no value. It's supposed to get a random row from $library, as determined by $rnd_research. However, it doesn't. $library['book_id'] has a value, but always the same one.
     
    egdcltd, Jun 28, 2007 IP
  6. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Found what was wrong

    This
    $library = $db->sql_fetchrow($result);
    PHP:
    should have been
    $library = $db->sql_fetchrowset($result);
    PHP:
     
    egdcltd, Jun 29, 2007 IP