php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54508 mysqli: truncated floats
Submitted: 2011-04-11 19:07 UTC Modified: 2011-04-13 11:53 UTC
From: anthon dot pang at gmail dot com Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.3.6 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: anthon dot pang at gmail dot com
New email:
PHP Version: OS:

 

 [2011-04-11 19:07 UTC] anthon dot pang at gmail dot com
Description:
------------
There's an inconsistency in behaviour between mysqli and PDO_MSYQL in the following SELECT, e.g.,

SELECT TRUNCATE(SUM(aFloatColumn),2) FROM aTable;

Let's say the sum of the column is 42.001.

With PDO_MYSQL, you'll get "42.00".

But with MYSQLI, you'll instead get "42".


Test script:
---------------
<?php
$link = mysqli_connect("localhost", "user", "password", "dbname");

$query = 'CREATE TABLE t (f FLOAT)';
$stmt = mysqli_prepare($link, $query);
mysqli_stmt_execute($stmt);

$query = 'TRUNCATE TABLE t';
$stmt = mysqli_prepare($link, $query);
mysqli_stmt_execute($stmt);

$query = 'INSERT INTO t (f) VALUES (42.001)';
$stmt = mysqli_prepare($link, $query);
mysqli_stmt_execute($stmt);

$query = "SELECT truncate(sum(f), 2) from t";
$stmt = mysqli_prepare($link, $query);
mysqli_stmt_execute($stmt);

mysqli_stmt_bind_result($stmt, $revenue);
while (mysqli_stmt_fetch($stmt)) {
    printf ("%s\n", $revenue);
}

mysqli_stmt_close($stmt);
mysqli_close($link);


Expected result:
----------------
42.00

Actual result:
--------------
42

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-13 11:53 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2011-04-13 11:53 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

PDO is using emulation of prepared statements by default. By such it is executing a regular "query" command which uses the MySQL "text" protocol. When using prepared statements with mysqli it is using true prepared statements and they are using the binary protocol for talking to the MySQL server. This means data is received in different formats and by such is interpreteded differently.
 [2011-04-14 13:55 UTC] anthon dot pang at gmail dot com
Thank you very much for the explanation.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC