Easy Tutorial
❮ Func Array Natcasesort Func Array Replace ❯

PDO::prepare

PHP PDO Reference Manual

PDO::prepare — Prepares an SQL statement for execution and returns 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 an SQL statement for execution with the PDOStatement::execute() method. The SQL statement can contain zero or more named (:name) or question mark (?) parameter markers, which will be replaced upon execution.

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

Parameters in the prepared SQL statement are passed as actual values when using the PDOStatement::execute() method.


Parameters

statement

driver_options


Return Value

If successful, PDO::prepare() returns a PDOStatement object. If it fails, it returns FALSE or throws a PDOException.


Examples

Preparing an SQL statement using named (:name) parameters

<?php
/* Passing values to the prepared statement via an array */
$sql = 'SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :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();
?>

Preparing an SQL statement using question mark (?) parameters

<?php
/* Passing values to the prepared statement via an array */
$sth = $dbh->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 Manual

❮ Func Array Natcasesort Func Array Replace ❯