0

I am fetching (with PHP PDO) from MySQL a value (stored in signed Float(7,2) format).

Result
=====================
Database = 8.9 //correct value
PHP = 8.8999996185303 //fetch from PHP PDO

Sample code:

$qry = 'SELECT hours FROM sample WHERE filter=?;';
$stmt = $db->prepare($qry);
$stmt->execute(array($filter));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo $row['hours'];
}

What is the best possible way to fetch this data from the DB? Ive read about the floating point approximalization issue in PHP. Thus I was thinking to perhaps change the SQL into something like:

SELECT (hours * 100) as butSoExpensiveOnDatabase etc...

Or using round or number_format in PHP. What would you guys suggest and why?

Changing the table structure is NOT an option!

(reason: old legacy code I need to support + no time + no budget)

1 Answer 1

2

You can do this

"SELECT round(hours, 2) FROM sample WHERE filter=?;"
Sign up to request clarification or add additional context in comments.

2 Comments

this works, but isnt there a PHP solution, I dont like to let the mysql server make lots of calculations
@Terence Using mathematical functions in MySQL is better than having PHP do so.

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.