|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-01-10 02:51 UTC] nbari at dalmp dot com
Description: ------------ --- From manual page: http://www.php.net/mysqli-stmt.bind-param#refsect1-mysqli- stmt.bind-param-parameters --- When inserting a 'double' using prepared statements d corresponding variable has type double if the number is on the format n.y example:1.3, or 99.3, an '0' is appended to the number, storing something like 1.30, or 99.30 instead of just 1.3 or 99.3 Test script: --------------- $mysqli = new mysqli('localhost','dalmp','test','test'); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $name = 99.3; if ($stmt = $mysqli->prepare('INSERT INTO test set name=?')) { $stmt->bind_param('d',$name); $stmt->execute(); $stmt->close(); } $mysqli->close(); #this will store 99.30 instead of just 99.3 Expected result: ---------------- do not alter/append the input with a 0 when selecting 'd' as the bind_parameter PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 02:00:01 2025 UTC |
The type of column 'name' is text() After inserting the data, I query via mysql console directly on a terminal and notice the 0, so i can confirm it is not an issue with a software displaying or formatting numbers with 2 decimal places. I made the same test using 'i' (for integer) and instead of 99.3 the stored value was just 99 (no 0 appended), when using 's' is is working but I start noticing this strange behaivor when the search string was an double. my search query/code is like this: $sql = 'select id from test where name=?'; $param = $_POST['name']; if (is_numeric($param)) { $param = !strcmp(intval($param), $param) ? (int)$param : (!strcmp(floatval($param), $param) ? (float)$param : $param); } $type = is_int($param) ? 'i' : (is_float($param) ? 'd' : (is_string($param) ? 's' : 'b')); $stmt->bind_param($type,$param); $stmt->execute(); $stmt->close(); Initially I though that the 'sanitation' made was affecting the results but later manually I specified the types and notice that only with doubles 'd' this was happening.