PHP Velho Oeste 2024

SQLite3Stmt::bindValue

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

SQLite3Stmt::bindValueパラメータの値を変数にバインドする

説明

public SQLite3Stmt::bindValue(string|int $param, mixed $value, int $type = SQLITE3_TEXT): bool

パラメータの値を変数にバインドします。

警告

PHP 7.2.14 と 7.3.0 より前のバージョンでは、 ステートメントがいったん実行されると、 バインドされたパラメータの値を変えられるようにするために SQLite3Stmt::reset() を呼び出す必要がありました。

パラメータ

param

バインドされるべき文中の値を識別する string (名前付きパラメータの場合) または (位置パラメータの場合) int。 名前付きパラメータがコロン (:) や アットマーク (@) ではじまっていない場合、コロンが自動的に名前の前に付加されます。 位置パラメータは 1 から始まります。

value

変数にバインドする値。

type

バインドする値のデータ型。

  • SQLITE3_INTEGER: 符号付き整数。 値の大きさに応じて 1, 2, 3, 4, 6, あるいは 8 バイトで格納されます。

  • SQLITE3_FLOAT: 浮動小数点数値。 8 バイトの IEEE 浮動小数点数値として格納されます。

  • SQLITE3_TEXT: テキスト文字列。 データベースのエンコーディング (UTF-8, UTF-16BE あるいは UTF-16-LE) を用いて格納されます。

  • SQLITE3_BLOB: blob データ。 入力がそのままの形式で格納されます。

  • SQLITE3_NULL: NULL 値。

PHP 7.0.7 以降では、type が省略されると、 自動的に value の型から検出します: boolint 型は SQLITE3_INTEGER として扱われ、 floatSQLITE3_FLOATnullSQLITE3_NULL、 そして、これら以外は全て SQLITE3_TEXT として扱われます。 これより前のバージョンでは、type が省略されると、 デフォルトの型 SQLITE3_TEXT になっていました。

注意:

valuenull の場合、 与えられた type に関わらず、 常に SQLITE3_NULL として扱われます。

戻り値

値を変数にバインドした場合に true、失敗した場合に false を返します

変更履歴

バージョン 説明
7.4.0 param が、新たに @param 記法をサポートしました。

例1 SQLite3Stmt::bindValue() の例

<?php
$db
= new SQLite3(':memory:');

$db->exec('CREATE TABLE foo (id INTEGER, bar STRING)');
$db->exec("INSERT INTO foo (id, bar) VALUES (1, 'This is a test')");

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

$result = $stmt->execute();
var_dump($result->fetchArray(SQLITE3_ASSOC));
?>

上の例の出力は以下となります。

array(1) {
  ["bar"]=>
  string(14) "This is a test"
}

参考

add a note add a note

User Contributed Notes 4 notes

up
30
zeebinz at gmail dot com
13 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
8 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
9 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=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));
    }
}
?>
up
1
vaibhavatul47 at gmail dot com
8 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