Bug #80445 [Opn]: Using float bind parameter returns weird error
| From: | domen at jollydeck dot com | Date: | Mon, 30 Nov 2020 03:26:21 +0000 |
| Subject: | Bug #80445 [Opn]: Using float bind parameter returns weird error | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-230718@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80445&edit=1
ID: 80445
User updated by: domen at jollydeck dot com
Reported by: domen at jollydeck dot com
Summary: Using float bind parameter returns weird error
Status: Open
Type: Bug
Package: PDO MySQL
Operating System: Windows, CentOS
PHP Version: 7.4.13
Block user comment: N
Private report: N
New Comment:
MySQL general log says the following:
Prepare CREATE TEMPORARY TABLE
test (field INT NOT NULL)
Execute CREATE TEMPORARY TABLE test (field INT NOT NULL)
Close stmt
Prepare INSERT INTO test (field) VALUES (0)
Execute INSERT INTO test (field) VALUES (0)
Close stmt
Prepare UPDATE test SET field = 100 * ?
Execute UPDATE test SET field = 100 * '0.3'
Close stmt
Indeed, when executing the following statements in mysqld, MySQL returns "ERROR 1292 (22007):
Truncated incorrect INTEGER value: '0.3'":
[create temporary table]
[insert value]
PREPARE stmt FROM 'UPDATE test SET field = 100 * ?';
SET @a = '0.3';
EXECUTE stmt1 USING @a;
So:
1. This is MySQL bug/unexpected behaviour.
2. Is SQL error 22007 ("Invalid datetime format") MySQL bug or is it wrongly described in
PHP?
3. Is there any workaround? I cannot find any PDO::PARAM_* value that corresponds to PHP's
float or MySQL's DECIMAL/FLOAT.
Previous Comments:
------------------------------------------------------------------------
[2020-11-30 02:52:00] danack@php.net
It might be useful to get the actual statement being executed. Apparently this can be done by
setting the a log entry in my.cnf according to: https://stackoverflow.com/a/1813818/778719
------------------------------------------------------------------------
[2020-11-30 01:06:35] domen at jollydeck dot com
Description:
------------
Following code produces weird error "SQLSTATE[22007]: Invalid datetime format: 1292 Truncated
incorrect INTEGER value" although no date or time is used anywhere.
This only happens when PDO::ATTR_EMULATE_PREPARES is set to false and bound parameter is float.
Table engine does not matter (tested with InnoDB and MyISAM).
Statement executes successfully if bind parameter is integer or the value "0.3" is not
bound but hard-coded into the query (either as '0.3' or as 0.3).
Test script:
---------------
<?php
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'test_username',
'test_password');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->query("CREATE TABLE test (field INT NOT NULL)
ENGINE=InnoDB");
$pdo->query("INSERT INTO test (field) VALUES (0)");
$stmt = $pdo->prepare("UPDATE test SET field = 100 * ?");
$stmt->execute(array(0.3));
Expected result:
----------------
Query executes successfully
Actual result:
--------------
Fatal error: Uncaught PDOException: SQLSTATE[22007]: Invalid datetime format: 1292 Truncated
incorrect INTEGER value: '0.3' in ...
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80445&edit=1