php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #43203 PDO better support for float bind values
Submitted: 2007-11-06 09:22 UTC Modified: 2011-02-21 20:54 UTC
Votes:37
Avg. Score:4.6 ± 0.7
Reproduced:37 of 37 (100.0%)
Same Version:7 (18.9%)
Same OS:7 (18.9%)
From: lafriks at inbox dot lv Assigned:
Status: Open Package: PDO related
PHP Version: 5.2.4 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-11-06 09:22 UTC] lafriks at inbox dot lv
Description:
------------
It would be way easier to work with PDO if there was PDO::PARAM_FLOAT. Otherwise there is always need to check for comma and point if in different locales and that just makes it unnecessary hard to work with float numbers and binding to prepared statements.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-21 20:54 UTC] jani@php.net
-Package: Feature/Change Request +Package: PDO related
 [2012-01-20 10:31 UTC] r dot wilczek at web-appz dot de
The missing PDO::PARAM_DOUBLE actually leads to very annoying problems.

SQLite considers a NUMERIC to be always less than a TEXT or REAL.
So string '1' is not less than double 1.1.

The following unparameterized statement shows this behaviour:
$rs = $sqlite->prepare('SELECT '1' < 1.1 as "result"');
$stmt->execute();
$rs = $stmt->fetch();
$stmt->closeCursor();
print_r($rs['result']); // 0 works as expected (and the same way as SQLite CLI.)

Now we try to perform the same operation using a parameterized statement:
$stmt = $sqlite->prepare('SELECT :left < :right as "result"');

Option A. Binding double as string:
$stmt->bindValue(':left', '1', PDO::PARAM_STR); 
$stmt->bindValue(':right', 1.1, PDO::PARAM_STR); 
$stmt->execute();
$rs = $stmt->fetch();
$stmt->closeCursor();
print_r($rs['result']); // 1 failure.  

Option B. Binding double as integer:
$stmt->bindValue(':left', '1', PDO::PARAM_STR); 
$stmt->bindValue(':right', 1.1, PDO::PARAM_INT);
$stmt->execute();
$rs = $stmt->fetch();
$stmt->closeCursor();
print_r($rs['result']); // 0 Seems to work, but ...

... this way we cannot tell double from integer. SQLite would consider
integer 1 to be less than double 1.1. 
$stmt->bindValue(':left', 1, PDO::PARAM_INT); 
$stmt->bindValue(':right', 1.1, PDO::PARAM_INT);
$stmt->execute();
$rs = $stmt->fetch();
$stmt->closeCursor();
print_r($rs['result']); // 0 Failure

So there is no way with PDO to correctly parameterize double values. 
I request to provide PDO::PARAM_DOUBLE.
 [2015-10-20 16:34 UTC] et at traede dot com
+1, this is a really annoying issue, and the only way to circumvent it is to walk manually through every result set and cast to float every value. I can't believe there has been no activity on this for almost 4 years
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Oct 17 00:01:27 2019 UTC