note 116523 added to sqlite3stmt.bindvalue
| From: | bohwaz | Date: | Sun, 18 Jan 2015 04:49:13 +0000 |
| Subject: | note 116523 added to sqlite3stmt.bindvalue | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201448@lists.php.net to get a copy of this message | ||
It might be a good idea to feed bindValue the type of the variable manually, or you might encounter
weird stuff as the passed value is often treated as SQLITE3_TEXT and results in buggy queries.
For example:
<?php
$st = $db->prepare('SELECT * FROM test WHERE (a+1) = ?');
$st->bindValue(1, 2);
?>
Will never return any result as it is treated by SQLite as if the query was 'SELECT * FROM test
WHERE (a+1) = "2"'. Instead you have to set the type manually:
<?php
$st = $db->prepare('SELECT * FROM test WHERE (a+1) = ?');
$st->bindValue(1, 2, \SQLITE3_INTEGER);
?>
And it will work. This bug is reported in https://bugs.php.net/bug.php?id=68849
Here is a simple function to help you make bindValue work correctly:
<?php
function getArgType($arg)
{
switch (gettype($arg))
{
case 'double': return SQLITE3_FLOAT;
case 'integer': return SQLITE3_INTEGER;
case 'boolean': return SQLITE3_INTEGER;
case 'NULL': return SQLITE3_NULL;
case 'string': return SQLITE3_TEXT;
default:
throw new \InvalidArgumentException('Argument is of invalid type
'.gettype($arg));
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 87.88.42.116
----
Manual Page -- http://php.net/manual/en/sqlite3stmt.bindvalue.php
Edit -- https://master.php.net/note/edit/116523
Del: integrated -- https://master.php.net/note/delete/116523/integrated
Del: useless -- https://master.php.net/note/delete/116523/useless
Del: bad code -- https://master.php.net/note/delete/116523/bad+code
Del: spam -- https://master.php.net/note/delete/116523/spam
Del: non-english -- https://master.php.net/note/delete/116523/non-english
Del: in docs -- https://master.php.net/note/delete/116523/in+docs
Del: other reasons-- https://master.php.net/note/delete/116523
Reject -- https://master.php.net/note/reject/116523
Search -- https://master.php.net/manage/user-notes.php