PDO::prepare — Prepares a SQL statement for execution and returns a PDOStatement object (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
public PDOStatement PDO::prepare ( string $statement [, array $driver_options = array() ] )
Prepare the SQL statement to be executed for the PDOStatement::execute() method. The SQL statement can contain zero or more named (:name) or question mark (?) parameter markers. The parameters will be replaced when the SQL is executed.
You cannot include both named (:name) or question mark (?) parameter markers in an SQL statement. You can only choose one of these styles.
The parameters in the prepared SQL statement will pass the real parameters when using the PDOStatement::execute() method.
statement is a legal SQL statement.
driver_options This array contains one or more key=>value pairs to set the properties of the PDOStatement object. The most common use is to set the PDO::ATTR_CURSOR value to PDO::CURSOR_SCROLL to request a scrollable cursor.
If successful, PDO::prepare() returns a PDOStatement object. If it fails, it returns FALSE or throws a PDOException.
<?php/* Pass values to prepared statements via array values*/$sql = 'SELECT name, colour, calories FROM fruit WHERE calories < :calories AND color = :colour';$sth = $dbh->prepare($ sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));$sth->execute(array(':calories' => 150, ':colour' => 'red'));$red = $sth->fetchAll();$sth->execute(array(':calories' => 175, ':colour' => 'yellow' ));$yellow = $sth->fetchAll();?>
<?php/* Pass values to prepared statements via array values*/$sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ? AND color = ?');$sth->execute (array(150, 'red'));$red = $sth->fetchAll();$sth->execute(array(175, 'yellow'));$yellow = $sth->fetchAll();?>