SQLite3Stmt::bindValue

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

SQLite3Stmt::bindValueVincular el valor de un parámetro a una variable de sentencia

Descripción

public SQLite3Stmt::bindValue(mixed $sql_param, mixed $value, int $type = ?): bool

Vincula el valor de un parámetro a una variable de sentencia.

Parámetros

sql_param

Un string o un int que identifica la variable de sentencia que debe ser vinculada al parámetro.

value

El valor a vincular a la variable de sentencia.

type

El tipo de dato de la variable a vincular.

  • SQLITE3_INTEGER: El valor es un entero con signo, almacenado en 1, 2, 3, 4, 6, u 8 bytes, dependiendo de la magnitud del valor.

  • SQLITE3_FLOAT: El valor es de tipo coma flotante, almacenado como un número de coma flotante IEEE de 8 bytes.

  • SQLITE3_TEXT: El valor es una cadena de texto, almacenado usando la codificación de la base de datos (UTF-8, UTF-16BE o UTF-16-LE).

  • SQLITE3_BLOB: El valor es de tipo blob, almacenado exactamente a como fue insertado.

  • SQLITE3_NULL: El valor es NULL.

Valores devueltos

Devuelve true si el valor es vinculado a la variable de sentencia, false en case de error.

Ejemplos

Ejemplo #1 Ejemplo de SQLite3Stmt::bindValue()

<?php
unlink
('mibdsqlite.db');
$bd = new SQLite3('mibdsqlite.db');

$bd->exec('CREATE TABLE foo (id INTEGER, bar STRING)');
$bd->exec("INSERT INTO foo (id, bar) VALUES (1, 'Esto es una prueba')");

$sentencia = $bd->prepare('SELECT bar FROM foo WHERE id=:id');
$sentencia->bindValue(':id', 1, SQLITE3_INTEGER);

$resultado = $sentencia->execute();
var_dump($resultado->fetchArray());
?>

add a note

User Contributed Notes 4 notes

up
30
zeebinz at gmail dot com
15 years ago
Note that this also works with positional placeholders using the '?' token:<?php$stmt = $db->prepare('SELECT * FROM mytable WHERE foo = ? AND bar = ?');$stmt->bindValue(1, 'somestring', SQLITE3_TEXT);$stmt->bindValue(2, 42, SQLITE3_INTEGER);?>Positional numbering starts at 1.
up
10
andrevanzuydam at gmail dot com
10 years ago
I just want to say again, Numbering for parameters starts at ONE!This has caught me out quite a few times!
up
3
bohwaz
10 years ago
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=68849Here is a simple function to help you make bindValue work correctly:<?phpfunction 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));    }}?>
up
1
vaibhavatul47 at gmail dot com
9 years ago
I used following logic to prepare statements, It handles both Values and Arrays ( taking help from bohwaz note) :<?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));        }    }foreach ($params as $index => $val) {                // indexing start from 1 in Sqlite statement                if (is_array($val)) {                    $ok = $stmt->bindParam($index + 1, $val);                } else {                    $ok = $stmt->bindValue($index + 1, $val, getArgType($val));                }                                if (!$ok) {                    throw new Exception("Unable to bind param: $val");                }            }?>
To Top