How do i get mysql_num_rows from a php function

Hi everyone.

sorry about this silly question, i feel embarressed to ask it.

how do i get the mysql_num_rows from a php function; i wish to use the extracted number with my page pagination.

i know that if i do the following i will get the num rows.

$sql = mysql_query("SELECT id,  FROM Members WHERE email_activated='1' ORDER BY id ASC");
$nr = mysql_num_rows($sql);

i also know that if i place the same in a function i can simply question the Mysql Num rows after the query is run. e.g

function  nationality_list (    )
			 global $dbc;
			$select = "  SELECT
	                  $from   = "    FROM
			Members ";
	      $query = $select.$from  ;
	     $result = mysqli_query ($dbc, $query);		
	      $nr = mysql_num_rows($result);
             return $result ; 		

two questions;

  1. is this the correct way to get the num rows

  2. if so , how do i extract the number from the PHP function.

is it possible to do two returns from a function. i.e

  $result = mysqli_query ($dbc, $query);		
	      $nr = mysql_num_rows($result);
             return $result ; 		
             return  $nr ;

warm regards


is it possible to do two returns from a function.


If I were you I’d return the rows as an array and simply use count();


function getMyData(){

$data = array(1,2);  // spoofing your mysql query results being returned as an array
return $data;

$my_stuff = getMyData();

echo "I just got ".  count($my_stuff) . " rows!";

foreach( $my_stuff as $stuff){
// etc

hello everyone

i first want to tyhank cups for his kind responce.

i just have one question from his reponse.

does the .count() function return the number of rows from the query or does it simply count everything that is returned from the query. it might be easier if i show u a sql function.

will the count fucntion return the number of rows from the search below i.e

$country_list       = country_list();

echo "  count( $country_list  )    ";

 function  country_list (    )
			 global $dbc;
			$select = "  SELECT
	                  country  ";
	                  $from   = "    FROM
			countrylist  ";
		 $query = $select.$from  ;
	         $result = mysqli_query ($dbc, $query);
	     return $result ;

If all you’re wanting to do is gather the number of rows from a query, then I suggest you use MySQL’s built in count() function. It’s quicker than the mysql_num_rows() function because we don’t have to build the result set first to determine the number of rows; but rather just gets the row count via the index column. Here’s an example:

$Query = mysqli_query($DB_Con, "SELECT COUNT(*) FROM table_name WHERE column = 'value'");
$Query_Count = mysqli_fetch_array($query, MYSQL_NUM);
echo $Query_Count[0]; #Outputes number of rows

So if we were to apply this to your (shortened) function above:

function country_list()
    global $dbc;
    $Query = mysqli_query($dbc, "SELECT COUNT(*) FROM countrylist");
    $Query_Count = mysqli_fetch_array($Query, MYSQL_NUM);
    return $Query_Count[0];

echo country_list();

Another thing to note is that it’s return is an integer, however its cast is a string. So when wanting to compare its output value, you’ll have to bear that in mind.

Why not simply mysqli_num_rows?

$country_list = country_list();
// Object oriented style
echo $country_list->num_rows;
// or Procedural style
echo mysqli_num_rows($country_list);

thank you to everyone for thier kind help. i have resolved the problem.

Would be nice if you can share in the forum about how you have solved your problem so that others having same kind of issue will get help!