PDOStatement::execute — execute a prepared statement (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
bool PDOStatement::execute ([ array $input_parameters ] )
Execute prepared statements. If the prepared statement contains parameter markers, you must choose one of the following options:
Call PDOStatement::bindParam() to bind PHP variables to parameter markers: if any, pass input values and obtain output values by associating variables bound to parameter markers
or pass an array as input parameter values only
input_parameters
An array with as many elements as the parameters bound to the SQL statement to be executed. All values are treated as PDO::PARAM_STR.
You cannot bind multiple values to a single parameter; for example, you cannot bind two values to a single named parameter in an IN() clause.
The bound values cannot exceed the specified number. If there are more key names in input_parameters than specified by the PDO::prepare() prepared SQL, this statement will fail with an error.
Returns TRUE on success, or FALSE on failure.
<?php/* Execute a prepared statement by binding PHP variables*/$calories = 150;$colour = 'red';$sth = $dbh->prepare('SELECT name, color, calories FROM fruit WHERE calories < :calories AND color = :colour');$sth->bindParam(':calories', $calories, PDO::PARAM_INT);$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);$sth->execute();?>
<?php/* Execute a prepared statement by passing an array containing the inserted values*/$calories = 150;$colour = 'red';$sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < :calories AND color = :colour');$sth->execute(array(':calories' => $calories, ':colour' => $colour));?>
<?php/* Execute a prepared statement by passing an array of inserted values*/$calories = 150;$colour = 'red';$sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ? AND color = ?');$sth->execute(array($calories, $colour));?>
<?php/* Execute a prepared statement by binding PHP variables*/$calories = 150;$colour = 'red';$sth = $dbh->prepare('SELECT name, color, calories FROM fruit WHERE calories < ? AND color = ?');$sth->bindParam(1, $calories, PDO::PARAM_INT);$sth->bindParam(2, $colour, PDO::PARAM_STR, 12);$sth->execute();?>
<?php/* Execute a prepared statement containing an IN clause using the values of an array */$params = array(1, 21, 63, 171);/* Create a placeholder filled with the same number of params String*/$place_holders = implode(',', array_fill(0, count($params), '?'));/* for $params For each value in the array, the statement to be prepared contains enough unnamed placeholders. When the statement is executed, the values in the $params array are bound to the placeholders in the prepared statement. This is different from using PDOStatement::bindParam() because it requires a reference variable. PDOStatement::execute() only acts as an alternative to binding by value. */$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");$sth->execute($params);?>