Req #69348 [Com]: mysql_real_escape_string produces invalid decimal numbers on some locales

From: Date: Fri, 22 Sep 2017 18:57:46 +0000
Subject: Req #69348 [Com]: mysql_real_escape_string produces invalid decimal numbers on some locales
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-211325@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69348&edit=1 ID: 69348 Comment by: seb35 at seb35 dot fr Reported by: mdrolc at gmail dot com Summary: mysql_real_escape_string produces invalid decimal numbers on some locales Status: Not a bug Type: Feature/Change Request Package: MySQL related Operating System: All PHP Version: 5.6.7 Block user comment: N Private report: N New Comment: I disagree this is not a bug: mysqli_real_escape_string is supposed to convert PHP literals to SQL literals, and the representation of the floating number 0.1 in SQL is not "0,1" but "0.1" (the result is really a string and not a PHP float which would then converted to a string during output in the test script). Below a test script to show the result of mysqli_real_escape_string is "0,1" and it does not result of some processing during output. As documented, the charset is relevant for the strings, but this is about the decimal mark and not the charset. I experienced this issue in PHP 5.6.30, 7.0.23, 7.1.9, 7.2.0RC2. I guess this is linked to bug #74779, where the result of an SQL statement implying a float is trucated to the integer part, possibly because of a wrong decimal separator. Test script: --------------- setlocale( LC_NUMERIC, 'fr_FR.UTF-8' ); $conn = new mysqli(); $number = 1/10; $result = $conn->real_escape_string( $number ); setlocale( LC_NUMERIC, 'C' ); var_dump( $result ); Expected result: ---------------- string(3) "0.1" Actual result: -------------- string(3) "0,1" Previous Comments: ------------------------------------------------------------------------ [2015-04-01 18:50:07] stas@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 Doesn't look like a mysql bug. See: setlocale(LC_ALL, 'sl_SI.UTF8'); var_dump((string)19.9); It's just how string conversion of floats works. ------------------------------------------------------------------------ [2015-04-01 14:19:53] mdrolc at gmail dot com Description: ------------ SQL only accepts floats with dot as the decimal separator. But running a float through mysql_real_escape_string() returns a string with comma as the decimal separator on some locale configurations. MySQL fails silently and just cuts off the decimal part. This behaviour can result in nasty hard to catch bugs because behaviour depends highly on server configuration. mysql_real_escape_string() should always cast types according to MySQL conventions which in this case means that decimal numbers should use dot as a decimal separator regardless of the locale setting. Test script: --------------- mysql_connect("localhost", "root", "dev"); //English, everything is fine setlocale(LC_ALL, 'en_US.UTF8'); var_dump(localeconv()['decimal_point']); //string(1) "." var_dump(mysql_real_escape_string(19.9)); //string(4) "19.9" //Slovene, (Use a locale that is installed on your system to successfully reproduce this) setlocale(LC_ALL, 'sl_SI.UTF8'); var_dump(localeconv()['decimal_point']); //string(1) "," var_dump(mysql_real_escape_string(19.9)); //string(4) "19,9" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69348&edit=1

« previous php.bugs (#211325) next »