PDOStatement::bindValue
PHP 教程
· 2019-01-28 19:27:42
PDOStatement::bindValue — 把一个值绑定到一个参数(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
说明
语法
bool PDOStatement::bindValue ( mixed $parameter , mixed $value [, int $data_type = PDO::PARAM_STR ] )
绑定一个值到用作预处理的 SQL 语句中的对应命名占位符或问号占位符。
参数
parameter
参数标识符。对于使用命名占位符的预处理语句,应是类似 :name 形式的参数名。对于使用问号占位符的预处理语句,应是以1开始索引的参数位置。
value
绑定到参数的值
data_type
使用 PDO::PARAM_* 常量明确地指定参数的类型。
返回值
成功时返回 TRUE, 或者在失败时返回 FALSE。
实例
执行一条使用命名占位符的预处理语句
<?php
/* 通过绑定的 PHP 变量执行一条预处理语句 */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindValue(':calories', $calories, PDO::PARAM_INT);
$sth->bindValue(':colour', $colour, PDO::PARAM_STR);
$sth->execute();
?>
执行一条使用问号占位符的预处理语句
<?php
/* 通过绑定的 PHP 变量执行一条预处理语句 */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->bindValue(1, $calories, PDO::PARAM_INT);
$sth->bindValue(2, $colour, PDO::PARAM_STR);
$sth->execute();
?>
点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html
500 Lines or Less
Amy Brown、Michael DiBernardo / 2016-6-28 / USD 35.00
This book provides you with the chance to study how 26 experienced programmers think when they are building something new. The programs you will read about in this book were all written from scratch t......一起来看看 《500 Lines or Less》 这本书的介绍吧!