When inserting decimal numbers through php into mysql database, it keeps rounding.
As I set decimal(10, 6), so the number 1.123456 rounds as 1.000000, but when inserting directly in phpmyadmin, it works just fine.
Data is fetched from xml and inserted into mysql, that really doesn’t matter but someone might wonder where data is coming from.
Should I do something in php with those decimalls? I saw the same problem on laracasts community forum, but there is no answers. Any help would be great.
That’s not what the decimal field type does. decimal(10,6) would accurately store any number from -9999.999999 to 9999.999999. It would not round.
So, as Rudy pointed out, the problem isn’t in your database.
Depends on what you’re trying to store. If you’re trying to accurately store the value “1.123456”, no, you shouldn’t do anything with them. If you’re trying to store the closest integer to your data, you would round the data using PHP and store the result; that said, if you’re intending on doing that, why is your database field a 6-decimal-place decimal and not just an int?
It’s not an int because it must be decimal. Values came from xml in that form, so it’s not allowed to store as int or whatever datatype. It only should be stored as decimal.
Yes, I realized that it has nothing to do with database, but still I can’t make it work.
class ExchangeRates
{
// have xml and json data
// this one goes with json, but it works same as xml
$i = 0;
foreach ($jsonData as $item) {
$unit = $item["Unit"];
$buyRateForeign = $item["buyRateForeign"];
$meanRate = $item["meanRate"];
$sellRateForeign = $item["sellRateForeign"];
$database = new Database();
$sql = "INSERT INTO currency_list (unit, source, buyRateForeign, meanRate, sellRateForeign) VALUES (:unit, :source, :buyRateForeign, :meanRate, :sellRateForeign)";
$stmt = $database->getConnection->prepare($sql);
$stmt->bindValue(':unit', $unti);
// etc...
$stmt->execute();
$i++;
}
}
If I use SELECT query, data will be returned as a string, so for a second I thought that be a reason for not working properly.