If I were you I’d return the rows as an array and simply use count();
eg
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
}
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
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];
}
#Usage:
echo country_list();
Edit:
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.