Count number of rows?

Daisy

New member
I have a table called avatars, it has id, series ... Now i want to count the number of avatars of each series.

I ran this code in mysql -> SELECT series, COUNT(*) as 'count' FROM avatars GROUP BY series LIMIT 0, 30

Then it showed me a table that has 2 columns (series and count), for example

series count

Nature 5
Celebrities 13

Now i want to display it on my page with php:

<?php

Code:
$sql = "SELECT series, COUNT(*) as 'count' FROM avatars GROUP BY series LIMIT 0, 30";

while ($result = mysql_fetch_array($sql)) {
echo "Name : ".$result[series]." returned ".$result[count]." rows";
}

?>

Something is wrong with the while part. Can you tell me how i should correct it??? Thanks.
 
The problem is you haven't executed your SQL statement, use the following code and this will work:

<?php

$sql = "SELECT series, COUNT(*) as 'count' FROM avatars GROUP BY series LIMIT 0, 30";
$rs = mysql_query($sql, $conn);
if ($rs){
while ($result = mysql_fetch_array($rs)) {
echo "Name : ".$result[series]." returned ".$result[count]." rows";
}
}

?>

Where $conn is your established connection to the MySQL database.
 
Back
Top