From: Barry on
Hi List ^_^

PHP 4.2.2
Redhat 7.3
Mysql 3.23.54

Ok here is the problem:
I try to summary float values from my database (mysql) in PHP.

code:

$sql = "SELECT * FROM somwehere WHERE 1";
$result = mysql_query($sql);
while ($assoc = mysql_fetch_assoc($result))
{
//adding values
$sum = $sum + $assoc["floatval"];
}
echo $sum; // echoes an INTEGER VALUE O_o

echoing the mysql value it is float like "123.45"

The type of the mysql value is "string"
setting the type to float doesnt help (settype())

Any ideas?

Greets
Barry

--
Smileys rule (cX.x)C --o(^_^o)
Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o)
From: Andrei on
When working with floats with php/mysql I had problems too... When
summing amounts I had errors so my solution was using decimal (20, 10)
type into mysql for storing amounts and when using sums in php I used bc
functions. This way u get exact calculations (depending on bcscale()
parameter.

Andy

Barry wrote:
> Hi List ^_^
>
> PHP 4.2.2
> Redhat 7.3
> Mysql 3.23.54
>
> Ok here is the problem:
> I try to summary float values from my database (mysql) in PHP.
>
> code:
>
> $sql = "SELECT * FROM somwehere WHERE 1";
> $result = mysql_query($sql);
> while ($assoc = mysql_fetch_assoc($result))
> {
> //adding values
> $sum = $sum + $assoc["floatval"];
> }
> echo $sum; // echoes an INTEGER VALUE O_o
>
> echoing the mysql value it is float like "123.45"
>
> The type of the mysql value is "string"
> setting the type to float doesnt help (settype())
>
> Any ideas?
>
> Greets
> Barry
>
From: Barry on
Andrei wrote:
> When working with floats with php/mysql I had problems too... When
> summing amounts I had errors so my solution was using decimal (20, 10)
> type into mysql for storing amounts and when using sums in php I used bc
> functions. This way u get exact calculations (depending on bcscale()
> parameter.


Thanks Andy sounds great.
Changed DB to decimal 10.2

Tried it, but using bcadd i get a value of 0.00.

If you have som sample code lying around somewhere that would be great,
or do you know what happened?

Barry
--
Smileys rule (cX.x)C --o(^_^o)
Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o)
From: Barry on
Barry wrote:
> Andrei wrote:
>
>> When working with floats with php/mysql I had problems too... When
>> summing amounts I had errors so my solution was using decimal (20, 10)
>> type into mysql for storing amounts and when using sums in php I used
>> bc functions. This way u get exact calculations (depending on
>> bcscale() parameter.
>
>
>
> Thanks Andy sounds great.
> Changed DB to decimal 10.2
>
> Tried it, but using bcadd i get a value of 0.00.
>
> If you have som sample code lying around somewhere that would be great,
> or do you know what happened?
>
> Barry
No problem anymore. solved it. but parsing it to an other function it
doesnt work.
oh well, at least it's calculating ^-^

Thanks for help :)

Barry

--
Smileys rule (cX.x)C --o(^_^o)
Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o)
From: Andrei on
Welcome,
Please note that using bc function variables will be of type string. So
a code working with numeric values like:

$a = 1;
if( $a )
{
...
}

it's ok but with bc functions:

$a = 14.5;
$b = -14.5;
$c = bcadd( $a, $b );
if( $c )
{
...
}

will not work as expected ( if( "0" ) is interpreted like a string that
is not null and so condition is interpreted as true.

Andy

Barry wrote:
> Barry wrote:
>> Andrei wrote:
>>
>>> When working with floats with php/mysql I had problems too...
>>> When summing amounts I had errors so my solution was using decimal
>>> (20, 10) type into mysql for storing amounts and when using sums in
>>> php I used bc functions. This way u get exact calculations (depending
>>> on bcscale() parameter.
>>
>>
>>
>> Thanks Andy sounds great.
>> Changed DB to decimal 10.2
>>
>> Tried it, but using bcadd i get a value of 0.00.
>>
>> If you have som sample code lying around somewhere that would be
>> great, or do you know what happened?
>>
>> Barry
> No problem anymore. solved it. but parsing it to an other function it
> doesnt work.
> oh well, at least it's calculating ^-^
>
> Thanks for help :)
>
> Barry
>