RE: [PHP] need help with PHP/MySQL
| From: | David Robley | Date: | Tue, 21 Nov 2000 04:49:33 +0000 |
| Subject: | RE: [PHP] need help with PHP/MySQL | ||
| References: | 1 2 | Groups: | php.db php.general |
| Request: | Send a blank email to php-db+get-4597@lists.php.net to get a copy of this message | ||
On Tue, 21 Nov 2000, Gustavo Souza wrote:
> SELECT SUM() dont work for me cause the fields have (R$) 300.00
> and not only the numbers ....
> i need to do it on php ...
>
> thanks
>
> GS
You should consider changing your database structure; there is no need to
store the currency symbol for every record, and it is only needed when you
want to display the values, so you can just echo it in front of the
relevant result. Using the SUM function in SQL is a great deal faster than
performing the calculation using a loop in PHP; as the size of your DB gets
larger, this has the potential to become a slow process.
But if you absolutely insist, recheck your value that is returned by
$price = substr ("$fullprice", 3);
If you echo $price as you go through the while loop, you may see something
unexpected :-)
>
> At 14:39 21/11/2000 +1000, you wrote:
> >Instead of all this, you should have SQL perform the SUM for you.
> >
> >"SELECT SUM(preco) from preco"
> >
> >This will sum all rows in the 'preco' table. You should put a where clause
> >in if you want to only sum some of the rows.
> >
> > > -----Original Message-----
> > > From: Gustavo Souza [mailto:gus74@terra.com.br]
> > > Sent: Tuesday, 21 November 2000 14:28
> > > To: php-general@lists.php.net
> > > Subject: [PHP] need help with PHP/MySQL
> > >
> > >
> > > oks, this code works for what i was looking for :
> > >
> > > $mysql = mysql_connect("host", "user", "pass");
> > >
> > > mysql_select_db(teste);
> > >
> > > $sqlquery = "SELECT * FROM preco";
> > >
> > > if ($resultset = mysql_query($sqlquery, $mysql))
> > > {
> > >
> > > echo "<TABLE>";
> > > while ($row = mysql_fetch_array($resultset))
> > > {
> > >
> > > /* Remove R$ from Preco field */
> > >
> > > $fullprice = ($row[preco]);
> > > $price = substr ("$fullprice", 3);
> > >
> > > $totalprice += ($price);
> > >
> > > }
> > > echo
> > > "<TR><TD>Total:</TD>"."<TD>".sprintf('%0.2f',
> > > $totalprice)."</TD>"."</TABLE>";
> > > }
> > >
> > > but this code just calculate the sum fom one row .. how can i
> > > calculate the
> > > all rows from a table ...
> > >
> > > thanks
> > >
> > > GS
> > >
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
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