PDOStatement::bindValue()
给占位符绑定一个值,可以指定类型。
PDOStatement::bindValue(string|int $param, mixed $value, int $type = PDO::PARAM_STR): bool
参数
| 参数 | 类型 | 说明 |
|---|---|---|
$param | string|int | 占位符,如 1 或 :name |
$value | mixed | 值 |
$type | int | PDO::PARAM_INT / STR / BOOL / NULL / LOB |
返回值
bool — 成功返回 true。
示例
$st = $pdo->prepare('SELECT * FROM articles LIMIT ? OFFSET ?');
$st->bindValue(1, $limit, PDO::PARAM_INT);
$st->bindValue(2, $offset, PDO::PARAM_INT);
$st->execute();注意
- LIMIT / OFFSET 在模拟预处理下不能用占位符,绑定整数类型可以解决。
- bindParam 是绑定变量引用,execute 时才取值;bindValue 是立即取值。循环里插入用 bindValue 更安全。