PHP mysql query from 2 tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nick_dad
    New Member
    • Mar 2006
    • 2

    PHP mysql query from 2 tables

    I have two tables I am trying to pull data from. The first table holds "groups" and the other table "group_memb ers" holds the members of that group with a respective id that points to id in "groups".

    I need to count the number of members (rows.. each row represents one membership) from group members, and print the 10 most populus groups with their total members.

    this is what i have for a query, but then i do not know how to fetch the array... I can't think right now either because i have been staring at this for hours!!

    here is my query:

    Code:
     
    $sql="SELECT group_id, count(*) as a FROM group_friends GROUP BY group_id ORDER BY COUNT(*) DESC";
      
    $res=mysql_query($sql)
    while($data=mysql_fetch_array($res)) {
    print data...
    }
    thanks for the help!!
    Last edited by Niheel; Mar 3 '06, 06:41 PM.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I think you have made a typo in your query, you have FROM group_friends but you have stated that you tables are called groups and group_members. I assume that you meant group_members. Also "AS a", a is not very descriptive how about "AS count" or "AS num".

    You have done most of the hard work retrieving the data, personally I would use mysql_fetch_ass oc rather than mysql_fetch_arr ay but it makes little difference.

    you can easily access the result in your variable data like this

    Code:
    echo( "<p>${data['group_id']} - ${data['a']}</p>\n" );
    should do it inside your while loop.

    Comment

    • nick_dad
      New Member
      • Mar 2006
      • 2

      #3
      Ok thanks ... i appreciate you taking the time to help!!

      Comment

      Working...