1

i am trying to save number of cents in MySql table as float , but the database is taking it wrong , for example i am saving 0.01, its saved as 0.1 !

is it possible to make this happen ?

My Code ;

$return["reward"] = 0.05;

$api->user['balance']  += $return["reward"];

$q = $api->pdo->prepare("UPDATE " . DB_PREFIX . "user
                         SET 
                         balance = :balance
                         WHERE 
                         userid = :userid");

$q->bindParam(":balance" , $api->user['balance']  , PDO::PARAM_INT );
$q->bindParam(":userid" , $api->user['userid'] , PDO::PARAM_INT); 

the balance column TYPE float in database.

11
  • What type does balance column have? Commented Dec 28, 2018 at 14:44
  • @MaximFedorov type of Float Commented Dec 28, 2018 at 14:46
  • 1
    Using PDO::PARAM_INT will save it using an integer, so not sure how this then comes out as 0.1. I would have expected either 0 or 1. Commented Dec 28, 2018 at 14:50
  • As @Curious_Mind said, you should use PDO::PARAM_STR, check this post: stackoverflow.com/questions/2718628/pdoparam-for-type-decimal Maybe your database config is wrong? Commented Dec 28, 2018 at 14:50
  • i Did changed it to PDO::PARAM_STR still same Commented Dec 28, 2018 at 14:53

3 Answers 3

1

This fault is not related to your code.

You're using the data type float which shouldn't be used to store monetary values.

Change your data type to decimal which is considered the best practise for storing monetary values.

You can change it through the webinterface of phpMyAdmin or with a query like so:

ALTER TABLE tablename MODIFY columnname DECIMAL(5,2);

This will allow you to store 5 digits before the comma and 2 after, change it to your needs.

Sign up to request clarification or add additional context in comments.

1 Comment

Your description of the digits before the comma is incorrect. DECIMAL(5,2) has a total number of digits of 5, with 2 for after the decimal point. dev.mysql.com/doc/refman/5.7/en/…
1

Try to change column type to decimal and set Length to 7.2 . Which will be seven numbers before the dot and 2 after it

Comments

1

Try with PDO::PARAM_STR instead of PDO::PARAM_INT.

$q->bindParam(":balance" , $return["reward"]  , PDO::PARAM_STR);

From Documentation

MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE PRECISION(M,D). Here, (M,D) means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. For example, a column defined as FLOAT(7,4) will look like -999.9999 when displayed. MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001

Also see : https://dev.mysql.com/doc/refman/5.5/en/problems-with-float.html

3 Comments

Then it is not related with PDO, it's your db column issue
the db column type set as Float , i've tried double also same XD
Yep, I think the same as Curious_Mind. Something is wrong in the database ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.