PDOStatement::execute
PDOStatement::execute — Execute a prepared statement (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
Description
Syntax
bool PDOStatement::execute ([ array $input_parameters ] )
Executes a prepared statement. If the prepared statement includes parameter markers, you must choose one of the following:
CallPDOStatement::bindParam()Bind PHP variables to the parameter markers: if any, pass input values and retrieve output values through variables bound to the associated parameter markers.
or pass an array of input-only parameter values.
Parameters
input_parameters
An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR.
Cannot bind multiple values to a single parameter; for example, cannot bind two values to a single named parameter in an IN() clause.
The number of bound values cannot exceed the number specified. If there are more keys in input_parameters than specified in the SQL prepared by PDO::prepare(), the statement will fail and emit an error.
Return Values
Returns TRUE on success, or FALSE on failure.
Examples
Execute a prepared statement with bound variables
<?php
/* 通过绑定 PHP 变量执行一条预处理语句 */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>
Execute a prepared statement with an array of insert values (named parameters)
<?php
/* 通过传递一个含有插入值的数组执行一条预处理语句 */
$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));
?>
Execute a prepared statement with an array of insert values (placeholders)
<?php
/* 通过传递一个插入值的数组执行一条预处理语句 */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array($calories, $colour));
?>
Execute a prepared statement with question mark placeholders
<?php
/* 通过绑定 PHP 变量执行一条预处理语句 */
$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();
?>
Execute a prepared statement with an IN clause using an array
<?php
/* 使用一个数组的值执行一条含有 IN 子句的预处理语句 */
$params = array(1, 21, 63, 171);
/* 创建一个填充了和params相同数量占位符的字符串 */
$place_holders = implode(',', array_fill(0, count($params), '?'));
/*
对于 $params 数组中的每个值,要预处理的语句包含足够的未命名占位符 。
语句被执行时, $params 数组中的值被绑定到预处理语句中的占位符。
这和使用 PDOStatement::bindParam() 不一样,因为它需要一个引用变量。
PDOStatement::execute() 仅作为通过值绑定的替代。
*/
$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");
$sth->execute($params);
?>
Other Extensions
PHP PDO Reference Manual