YouTip LogoYouTip

Pdo Prepare

# PDO::prepare [![Image 3: PHP PDO Reference](#)PHP PDO Reference](#) 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 by the PDOStatement::execute() method. The SQL statement can contain zero or more named (:name) or question mark (?) parameter markers. Parameters are replaced during SQL execution. You cannot use 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 real values when using the PDOStatement::execute() method. * * * ## Parameters **statement** A valid SQL statement. **driver_options** This array contains one or more key=>value pairs to set PDOStatement object attributes. The most common use is to set PDO::ATTR_CURSOR to PDO::CURSOR_SCROLL to request a scrollable cursor. * * * ## 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 with named (:name) parameters ## Example <?php/* Pass values to the prepared statement via an array */$sql = 'SELECT name, colour, calories FROM fruit WHERE calories 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 with question mark (?) parameters ## Example prepare('SELECT name, colour, calories FROM fruit WHERE calories execute(array(150, 'red')); $red = $sth->fetchAll(); $sth->execute(array(175, 'yellow')); $yellow = $sth->fetchAll(); ?> [![Image 4: PHP PDO Reference](#)PHP PDO Reference](#)
← Pdo QueryPdo Lastinsertid β†’