count how many times the same text shows up in a column

Discussion in 'PHP' started by lektrikpuke, Jul 1, 2009.

  1. #1
    Hi y'all,

    I'm trying to get a row count of text in a column from a DB. So far I have (and pardon my not so good coding):

    for ($i=1; $i<500; $i++){
    $get_query = mysql_query("SELECT column FROM tbl WHERE id = $i LIMIT 1");
    $display_query = mysql_fetch_array($get_query);
    $query_show = $display_query['column'];

    $query_count_1 = mysql_query("SELECT column FROM tbl WHERE column LIKE '" . $query_show . "'");
    $display_count = mysql_num_rows($query_count_1);
    if($display_count > 1 && $query_show != ''){
    echo $query_show . " - Matches: " . $display_count . "<br />";
    }
    }

    This almost does what I want in that it gets the text from row1 and searches all other rows and gives a total (if greater than 1) then goes to row2 and searches all the rows and gives a total, etc.

    However, what I want it to do is give the total and term only once. Close but no cookie. =(

    Any guidance would be appreciated. :)

    Rob
     
    lektrikpuke, Jul 1, 2009 IP
  2. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #2
    :) Thats not the way to do it AT ALL.

    Here try this

    $get_query = mysql_query("SELECT column, count(column) as myCount GROUP BY column ORDER BY myCount  desc");
    while($row = mysql_fetch_array($get_query))
    {
    echo $row['column'] . " - Matches: " . $row['myCount'] . "<br />";
    }
    PHP:
    That is all the code you will need.

    Amit
     
    samyak, Jul 1, 2009 IP
  3. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #3
    Thank you. Works great. Thanks again!!! :D
     
    lektrikpuke, Jul 1, 2009 IP
  4. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #4
    Glad I could help. :)
     
    samyak, Jul 2, 2009 IP