Re: [PHP4BETA] re:Totalling a variable in a while loop

From: Date: Mon, 21 Feb 2000 23:51:24 +0000
Subject: Re: [PHP4BETA] re:Totalling a variable in a while loop
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-10627@lists.php.net to get a copy of this message
On 22 Feb, Joseph H Blythe wrote: > Joseph H Blythe wrote: > >> David Robley wrote: >> >> > SELECT SUM(ItemTotal) AS grand_total FROM table WHERE UserId = 123 >> > >> > should do the trick. >> > >> > > How do I set an alias to the SUM? >> > >> > That's the 'AS grand_total' part above ;-) When you reference it from >> > PHP use the name grand_total or whatever you choose to call it. You need >> > to do that, aliasing, for any of the mysql functions like count, min, >> > max etc. >> > >> >> funnily enough this is exactly what I had written except in php like follows: >> >> mysql_query("SELECT SUM(ItemTotal) AS grand_total FROM Items WHERE >> UserID='$Cookie'"); >> >> this doesn't seem to work the total of all the columns under Items that belong >> to UserID (which is an cookie) should be in $grand_total? >> >> does this work on multiple rows? It seems to return nothing at all? Why? >> > > This works as a straight MYSQL command > > SELECT SUM(ItemTotal) AS grandTotal FROM Items WHERE > UserID='cc2b438a843e93612ea3a4d5936ee547'; > > But I think the problem might lie in the alias or something how do I access it > through php $grand_total? It returns "" > The value $Cookie does hold the right string, so I don't see what else could be > causing this? Use the alias as the field name in whatever method you are using to retrieve the data (there will only be one row, it seems). So maybe something like: $result = mysql_db_query($database, "SELECT SUM(ItemTotal) AS grandTotal FROM Items WHERE UserID='cc2b438a843e93612ea3a4d5936ee547'"); $row = mysql_fetch_array($result); echo "Total items: " . $row["grandTotal"] . '<BR>'; Erm, that's untested; if there are any syntax errors in there you can keep them :-) Cheers -- David Robley | WEBMASTER & Mail List Admin RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/ AusEinet | http://auseinet.flinders.edu.au/ Flinders University, ADELAIDE, SOUTH AUSTRALIA

« previous php.version4 (#10627) next »