X-Original-To: pgsql-php-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 9368611F6202 for ; Sat, 29 Apr 2006 18:32:12 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 98306-04-3 for ; Sat, 29 Apr 2006 18:31:56 -0300 (ADT) X-Greylist: delayed 00:23:40.34177 by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 57E0211F6235 for ; Sat, 29 Apr 2006 18:31:51 -0300 (ADT) Received: from twobarks.com (ns104.twobarks.com [80.65.57.37]) by svr4.postgresql.org (Postfix) with ESMTP id D51185B57BC for ; Sat, 29 Apr 2006 21:08:10 +0000 (GMT) Received: from [10.0.0.55] (cD9089068.sdsl.catch.no [217.8.144.104]) by twobarks.com (Postfix) with ESMTP id DE89C564005; Sat, 29 Apr 2006 23:09:36 +0200 (CEST) Message-ID: <4453D5B4.3070207@start.no> Date: Sat, 29 Apr 2006 23:08:04 +0200 From: Tommy Gildseth User-Agent: Debian Thunderbird 1.0.2 (X11/20051002) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Reed Loefgren Cc: pgsql-php@postgresql.org Subject: Re: PHP Newbie- Display Aggregates in HTML Table References: <20060426073420.Q3269@auden.jmla.com> In-Reply-To: <20060426073420.Q3269@auden.jmla.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200604/29 X-Sequence-Number: 3499 Reed Loefgren wrote: > All, > > I have a query that returns data that includes a sum(). I can't get this > sum to display in an html table. Like so (in part): > > Example bit: > > psql test: select code, blah, sum(time) from test; > > Portion of PHP table code: > > while($myrow = pg_fetch_assoc($result)) { > printf ("%s%s%s", > $myrow['code'], $myrow['blah'], $myrow['']); To display the result of an function, it's usually best to alias the value, like so: select code, blah, sum(time) AS the_sum from test; That way, you can use $myrow['the_sum'] to access that value. You could also do $myrow['sum(time)'], but it doesn't, imo, look very nice. The reason why your query doesn't work, is because you're trying to use an agregate function without a group by clause. ...Which would work fine, if you did just SELECT sum(time) FROM test, but not when you do SELECT code, blah, sum(time) FROM test. Tommy