SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2000
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the following piece of code where I populate a table from a mysql table I don't know how to reference the sum column

    $sql = "select
    sum(vote1c+vote2) , vote_name
    from votes
    where voteid = 1
    group by vote_name
    order by 1 desc , vote_name"; };


    $result = mysql_query($sql) or die("Couldn't execute query.");

    printf("<TABLE WIDTH=\"100%%\" BGCOLOR =\"#9999FF\" NOSAVE>\n");
    printf("<TR>
    <TD><B>No of votes</B></TD>
    <TD><B>vote name</B></TD>
    </TR>\n");
    printf("<TR>
    <TD><B>* </B></TD>
    <TD><B>* </B></TD>
    </TR>\n");
    while (($row=mysql_fetch_object($result))){
    printf("<TR>
    <TD>%s</TD>
    <TD>%s</TD>
    </TR>\n",
    $row->????,
    $row->vote_name) ;
    }
    printf("</TABLE>\n");


    How do I reference the sum column within the table column. Its got me stumped !

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Use a column alias:

    Code:
    $sql = "select 
    sum(vote1c+vote2) as vote_sum, vote_name 
    from votes 
    where voteid = 1 
    group by vote_name 
    order by 1 desc , vote_name"; }; 
    
    [...]
    
    while (($row=mysql_fetch_object($result))){ 
      printf("<TR> 
      <TD>%s</TD> 
      <TD>%s</TD> 
      </TR>\n", 
      $row->sum_column, 
      $row->vote_name) ; 
    } 
    printf("</TABLE>\n");
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Babys got backa hard back, that is: The Ultimate CSS Reference

  3. #3
    SitePoint Member
    Join Date
    Sep 2000
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks - that has worked a treat.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •