Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which SQL type should I use to input the value 4.865472349

Tags:

types

sql

php

mysql

PHP runs a script that correctly calculates a value. When I echo that value out it comes out as 4.865472349

Then a simple update script is used to enter the value into my database.

$query = "UPDATE members
     SET rating = $r 
     WHERE username = '$username'";
mysql_query($query);

When I do this, the value that is entered into the database is 5.

If I replace $r in the previous formula with 4.865472349 directly, it produces the same result.

Clearly this is because my SQL type was set to "integer"

But Im not sure what to change it to in order to fix this issue. Any help?

like image 300
user187680 Avatar asked Aug 29 '12 16:08

user187680


1 Answers

If you want the same precision as 4.865472349, you can use DECIMAL(10,9)

Reference

For example, a DECIMAL(18,9) column has nine digits on either side of the decimal point, so the integer part and the fractional part each require 4 bytes. A DECIMAL(20,6) column has fourteen integer digits and six fractional digits. The integer digits require four bytes for nine of the digits and 3 bytes for the remaining five digits. The six fractional digits require 3 bytes.

Assuming you are using MySql

like image 150
Momo Avatar answered Oct 27 '22 00:10

Momo