PDOStatement :: execute
PDOStatement :: execute - execute a prepared statement (PHP 5> = 5.1.0, PECL pdo> = 0.1.0)
Explanation
grammar
bool PDOStatement::execute ([ array $input_parameters ] )
Implementation of prepared statements. If prepared statements containing parameter markers, you must select one of the following practices:
Call PDOStatement :: bindParam () to bind PHP variables to the parameter markers: if any, to pass through the input values and variables associated parameter markers bound to obtain an output value
Or just as a passing input parameter value array
parameter
input_parameters
A number of elements and parameters of the SQL statement to be executed as many bindings array. All values are treated as PDO :: PARAM_STR.
You can not bind multiple values to a single argument; for example, you can not bind two values to the IN () clause in a single named parameter.
Bound value can not exceed the specified number. If there is more than PDO :: prepare () and more key name specified in the SQL preprocessing input_parameters, then this statement will fail with an error.
return value
Successful return TRUE, or on failure returns FALSE.
Examples
Execute a prepared statement bind 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(); ?>
Use an array containing the insert value is executed as a prepared statement (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)); ?>
Use an array containing the insert value is executed as a prepared statement (placeholders)
<?php /* 通过传递一个插入值的数组执行一条预处理语句 */ $calories = 150; $colour = 'red'; $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ?'); $sth->execute(array($calories, $colour)); ?>
Implementation of a question mark placeholders in the prepared statement
<?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(); ?>
Using arrays to execute a prepared statement containing the IN clause
<?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); ?>