PHP/MySQL problem

Discussion in 'PHP' started by sparckyz, Feb 11, 2007.

  1. #1
    I'v got this small PHP/MySQL problem which for some reason i just can't seem to get past.

    I've got this table with a field called for example "topic" and for each entry there may be different topics or the same topics e.g.

    id: topics:
    1 fish
    2 fish
    3 beans
    4 bread
    5 bread

    What i want to do is to count up the occurrences of each entry, but if they are the same then print them like this them like this:

    fish 2
    beans 1
    bread 2

    Sounds simple enough? if i've explained it at all well, but for some reason i cant seem to get it lol. I would show what code i have got, but its messy and i've changed it about so much that it probably looks silly :rolleyes:

    Any help would be appreciated?
     
    sparckyz, Feb 11, 2007 IP
  2. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #2
    $query = "SELECT topics, COUNT(topics) FROM topic GROUP BY topics"; 
    	 
    $result = mysql_query($query) or die(mysql_error());
    
    while($row = mysql_fetch_array($result)){
    	echo $row['topics']." : ".$row['COUNT(name)'];
    	echo "<br />";
    }
    PHP:
     
    YIAM, Feb 11, 2007 IP
  3. sparckyz

    sparckyz Peon

    Messages:
    336
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks very much YIAM.

    That is exactly what i was after ;)
     
    sparckyz, Feb 11, 2007 IP