Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PDOStatement::execute

PDOStatement::execute

PHP PDO Reference ManualPHP PDO Reference Manual

PDOStatement::execute - Execute a prepared statement (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)


Description

Syntax



bool PDOStatement::execute ([ array $input_parameters ] )

Execute a prepared statement. If the prepared statement contains parameter markers, you must choose one of the following approaches:


Parameters

** input_parameters**

An array with the same number of elements as the parameters bound in 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 number of bound values cannot exceed the specified count. If there are more keys in input_parameters than specified by the SQL prepared by PDO::prepare(), the statement will fail and issue an error.


Return Value

Returns TRUE on success, or FALSE on failure.


Example

Execute a prepared statement with bound variables



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 using an array containing inserted values (named parameters)



prepare('SELECT name, colour, calories

    FROM fruit

    WHERE calories < :calories AND colour = :colour');

$sth->execute(array(':calories' => $calories, ':colour' => $colour));

?>

Execute a prepared statement using an array containing inserted values (placeholders)



prepare('SELECT name, colour, calories

    FROM fruit

    WHERE calories < ? AND colour = ?');

$sth->execute(array($calories, $colour));

?>

Execute a prepared statement with question mark placeholders



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 containing an IN clause using an array



prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");

$sth->execute($params);

?>


PHP PDO Reference ManualPHP PDO Reference Manual