Add to Favorites
Hosting Discussion
 

forgot password?


Reply


Old
  Post #1 (permalink)   07-24-2005, 08:11 PM
HD Newbie
 
Join Date: Jun 2005
Location: Ontario
Posts: 25

Status: Daisy is offline
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.
__________________
Sincerely,

Daisy
 
 
 


Old
  Post #2 (permalink)   10-24-2005, 08:17 AM
HD Newbie
 
Join Date: Oct 2005
Posts: 5

Status: pfwh is offline
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.
 
 
 
Reply

Thread Tools

New Post New Post   Old Post Old Post
Posting Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On