PHP Newbie- Display Aggregates in HTML Table

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 ("<tr><td>%s</td><td>%s</td><td>%s</td></tr>",
$myrow['code'], $myrow['blah'], $myrow['<what goes here?>']);


I'm sure there's errors here, and bad coding too, but it works just fine
if I don't have sum() or count(*) in the query. And the query works fine
with sums and counts as long as I don't try and execute it in PHP. So my
coding is wrong/uneducated. What's amiss here?

Thanks,

r

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend
Reed Loefgren [ Mi, 26 April 2006 15:50 ] [ ID #1294657 ]

Re: PHP Newbie- Display Aggregates in HTML Table

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 ("<tr><td>%s</td><td>%s</td><td>%s</td></tr>",
> $myrow['code'], $myrow['blah'], $myrow['<what goes here?>']);


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

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend
Tommy Gildseth [ Sa, 29 April 2006 23:08 ] [ ID #1294658 ]
Datenbanken » gmane.comp.db.postgresql.php » PHP Newbie- Display Aggregates in HTML Table

Vorheriges Thema: PostgreSQL 8.1 + PHP5.1.x/4.4.2 + Apache 2.0.55/1.3.34 PROBLEM!!PLEASE HELP
Nächstes Thema: where to place the PHP scripts...