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

PDO::prepare

PDO::prepare

PHP PDO Reference ManualPHP PDO Reference Manual

PDO::prepare - Prepare the SQL statement to be executed and return a PDOStatement object (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)


Description

Syntax



public PDOStatement PDO::prepare ( string $statement [, array $driver_options = array() ] )

Prepares 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, which are replaced when the SQL is executed.

You cannot include both named (:name) and question mark (?) parameter markers in the same SQL statement; you must choose one style.

Parameters in a prepared SQL statement are passed real values when the PDOStatement::execute() method is used.


Parameters

statement
A valid SQL statement.

driver_options
This array contains one or more key=>value pairs to set properties of the PDOStatement object; the most commonly used is setting PDO::ATTR_CURSOR to PDO::CURSOR_SCROLL to request a scrollable cursor.


Return Value

On success, PDO::prepare() returns a PDOStatement object; on failure it returns FALSE or throws a PDOException.


Example

Prepare an SQL statement using named (:name) parameters

Example

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 ( ) ; ?>

Prepare an SQL statement using question mark (?) parameters

Example

prepare ( ' SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ? ' ) ; $sth -> execute ( array ( 150 , ' red ' ) ) ; $red = $sth -> fetchAll ( ) ; $sth -> execute ( array ( 175 , ' yellow ' ) ) ; $yellow = $sth -> fetchAll ( ) ; ?>

PHP PDO Reference ManualPHP PDO Reference Manual