PDOStatement::bindValue
(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 1.0.0)
PDOStatement :: bindValue - 为参数绑定一个值
描述
public bool PDOStatement::bindValue ( mixed $parameter , mixed $value [, int $data_type = PDO::PARAM_STR ] )
将值绑定到用于准备语句的SQL语句中的相应命名或问号占位符。
参数
parameter
参数标识符。对于使用命名占位符的预准备语句,这将是表单的参数名称:name。对于使用问号占位符的准备好的语句,这将是参数的1索引位置。
value
要绑定到参数的值。
data_type
使用PDO :: PARAM_ *常量的参数的显式数据类型。
返回值
成功时返回TRUE
或失败时返回FALSE
。
示例
示例#1使用命名占位符执行准备好的语句
<?php
/*?Execute?a?prepared?statement?by?binding?PHP?variables?*/
$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();
?>
示例#2使用问号占位符执行准备好的语句
<?php
/*?Execute?a?prepared?statement?by?binding?PHP?variables?*/
$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();
?>
扩展内容
- PDO :: prepare() - 准备执行语句并返回一个语句对象
- PDOStatement :: execute() - 执行准备好的语句
- PDOStatement :: bindParam() - 将参数绑定到指定的变量名称
← PDOStatement::bindParam
PDOStatement::closeCursor →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com