need a hint on query

Discussion in 'PHP' started by SnoozZz, Jan 23, 2009.

  1. #1
    If any body could just give me a hint on this.

    I have a table called 'gents_names' filled with many names.

    What I need to do is count how many times the names appear in the table.

    Example:
    Name count

    fred 10
    chris 5
    jack 16


    any help? please
     
    SnoozZz, Jan 23, 2009 IP
  2. fulltilt

    fulltilt Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Select name, count(*)
    from gents_names
    group by name

    that should work, but "name" might be a reserved word, depending on what SQL engine you're using.
     
    fulltilt, Jan 23, 2009 IP
  3. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks, its a bit more complexed than that, I used names etc to explain better.

    Here is what I have thus far.
    $result = mysql_db_query($database, "select refid, count(*) from aff_productsales WHERE adid = '".$_SESSION['aff_valid_admin']."' AND number = '$change' GROUP BY refid") 
        or die ("Database Error"); 
      
    	if (mysql_num_rows($result)) 
      {
        while ($qry = mysql_fetch_array($result)) 
    {
    print "<font face=arial><TABLE border=1 cellspacing=0 cellpadding=3 align=center>";
        print "<TR><TH>Refferer</TH><TH>Click Count</TH>";
        print "</TR>";
       
          print "<TR>";
          print "<TD><font size=2>";
          print $qry[refid];
          print "</TD>";
          print "<TD><font size=2>";
          print "xxxxx";
          print "</TD>";
          print "</TR>";
      
        print "</TABLE>";
      }
    }
    PHP:
    what would xxxx value be?
     
    SnoozZz, Jan 23, 2009 IP
  4. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #4
    select refid, count(*) as 'my_counter' from aff_productsales WHERE adid = '".$_SESSION['aff_valid_admin']."' AND number = '$change' GROUP BY refid"

    so your colname becomes 'my_counter' and you can use $qry['my_counter']. $qry[refid] should also be $qry['refid'], along with some other bad practises in your code (font tags? uppercase tags? no database class/functions?)
     
    falcondriver, Jan 23, 2009 IP
  5. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks, that worked. I appologise if my code offends, I'm part time self taught. But I appreciate your help. I'll fix what you mentioned.
     
    SnoozZz, Jan 23, 2009 IP