首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

PDOStatement::execute

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)

PDOStatement :: execute - 执行准备好的语句

描述

代码语言:javascript
复制
public bool PDOStatement::execute ([ array $input_parameters ] )

执行准备好的语句。如果准备的语句包含参数标记,则:

  • 必须调用PDOStatement :: bindParam()和/或PDOStatement :: bindValue()以将变量或值(分别)绑定到参数标记。绑定变量将其值作为输入,并接收其相关参数标记的输出值(如果有的话)
  • 或必须传递仅有输入参数值的数组

参数

input_parameters

一组具有与正在执行的SQL语句中的绑定参数一样多的元素的值的数组。所有的值都被视为PDO::PARAM_STR

多个值不能绑定到单个参数; 例如,不允许将两个值绑定到IN()子句中的单个命名参数。

绑定比指定更多的值是不可能的; 如果存在input_parameters比PDO :: prepare()中指定的SQL 更多的键,那么语句将失败并发出错误信息。

返回值

成功则返回TRUE或失败时返回FALSE

更新日志

版本

描述

5.2.0

input_parameters中的键必须与SQL中声明的键匹配。在PHP 5.2.0之前,这被默默忽略了。

示例

示例#1 使用绑定变量和值执行准备好的语句

代码语言:javascript
复制
<?php
/*?Execute?a?prepared?statement?by?binding?a?variable?and?value?*/
$calories?=?150;
$colour?=?'gre';
$sth?=?$dbh->prepare('SELECT?name,?colour,?calories
????FROM?fruit
????WHERE?calories?<?:calories?AND?colour?LIKE?:colour');
$sth->bindParam(':calories',?$calories,?PDO::PARAM_INT);
$sth->bindValue(':colour',?"%{$colour}%");
$sth->execute();
?>

示例#2 使用插入值数组(名为参数)执行准备好的语句

代码语言:javascript
复制
<?php
/*?Execute?a?prepared?statement?by?passing?an?array?of?insert?values?*/
$calories?=?150;
$colour?=?'red';
$sth?=?$dbh->prepare('SELECT?name,?colour,?calories
????FROM?fruit
????WHERE?calories?<?:calories?AND?colour?=?:colour');
$sth->execute(array(':calories'?=>?$calories,?':colour'?=>?$colour));
?>

示例#3 使用插入值数组(占位符)执行准备好的语句

代码语言:javascript
复制
<?php
/*?Execute?a?prepared?statement?by?passing?an?array?of?insert?values?*/
$calories?=?150;
$colour?=?'red';
$sth?=?$dbh->prepare('SELECT?name,?colour,?calories
????FROM?fruit
????WHERE?calories?<???AND?colour?=??');
$sth->execute(array($calories,?$colour));
?>

示例#4 使用问号占位符执行准备好的语句

代码语言:javascript
复制
<?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->bindParam(1,?$calories,?PDO::PARAM_INT);
$sth->bindParam(2,?$colour,?PDO::PARAM_STR,?12);
$sth->execute();
?>

示例#5 使用IN子句的数组执行准备好的语句

代码语言:javascript
复制
<?php
/*?Execute?a?prepared?statement?using?an?array?of?values?for?an?IN?clause?*/
$params?=?array(1,?21,?63,?171);
/*?Create?a?string?for?the?parameter?placeholders?filled?to?the?number?of?params?*/
$place_holders?=?implode(',',?array_fill(0,?count($params),?'?'));

/*
????This?prepares?the?statement?with?enough?unnamed?placeholders?for?every?value
????in?our?$params?array.?The?values?of?the?$params?array?are?then?bound?to?the
????placeholders?in?the?prepared?statement?when?the?statement?is?executed.
????This?is?not?the?same?thing?as?using?PDOStatement::bindParam()?since?this
????requires?a?reference?to?the?variable.?PDOStatement::execute()?only?binds
????by?value?instead.
*/
$sth?=?$dbh->prepare("SELECT?id,?name?FROM?contacts?WHERE?id?IN?($place_holders)");
$sth->execute($params);
?>

注意点:

注意:有些驱动程序在执行下一条语句之前需要关闭游标。

扩展内容

  • PDO :: prepare() - 准备执行语句并返回一个语句对象
  • PDOStatement :: bindParam() - 将参数绑定到指定的变量名称
  • PDOStatement :: fetch() - 从结果集里获取下一行
  • PDOStatement :: fetchAll() - 返回一个包含所有结果集行的数组
  • PDOStatement :: fetchColumn() - 返回结果集的下一行中的单个列

← PDOStatement::errorInfo

PDOStatement::fetch →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com