I have a mysql table, with only one register, where the price
field is of decimal(6,2)
type, with the value of 60.35
Consider the code above:
$prices = mysql_query("SELECT id, price FROM products");
while ($pr = mysql_fetch_assoc($prices)) {
$prics[$pr['id']]=$pr['price'];
echo $pr['price'].' - '.$prics[$pr['id']]."
";
}
The output is incorrect:
60.35 - 6
The correct would be:
60.35 - 60.35
I tried the same situation without mysql query:
$var = array(60.35,'60.35',60);
$var2[0] = $var[0];
$var2[1] = $var[1];
print_r($var2);
And it works as expected:
Array
(
[0] => 60.35
[1] => 60.35
)
How can I solve that problem?
I think that the problem is that PHP converts the second float to a string because it's concatenated to a string while it consider the first one a float
EDIT - no this works perfectly, it must be something else
The problem line is likely
$prics[$pr['id']]=$pr['price'];
Try forcing the type like:
settype($prics[$pr['id']], "float");
$prics[$pr['id']]=$pr['price'];
echo $pr['price'].' - '.$prics[$pr['id']]."
";
I solved the problem by initializing the array var before its use:
$prics = array();