PDO :: prepare
PDO :: prepare - prepare to execute SQL statement and returns a PDOStatement Objects (PHP 5> = 5.1.0, PECL pdo> = 0.1.0)
Explanation
grammar
public PDOStatement PDO::prepare ( string $statement [, array $driver_options = array() ] )
To PDOStatement :: execute () method prepares to execute SQL statements, SQL statements can contain zero or more named (: name) or a question mark parameter markers, parameters will be replaced when SQL execution (?).
You can not contain both named in the SQL statement (: name) (?) Or a question mark parameter markers can only choose one style.
Pretreatment SQL statement parameter when using PDOStatement :: execute () method passes the real parameters.
parameter
statement
Valid SQL statement.
driver_options
This array contains one or more key => value pair to set PDOStatement object's properties, is most commonly used to PDO :: ATTR_CURSOR value to PDO :: CURSOR_SCROLL to request a scrollable cursor.
return value
If successful, PDO :: prepare () returns PDOStatement object returns FALSE if it fails or throws an exception PDOException.
Examples
Use a named (: name) parameter to prepare SQL statements
<?php /* 通过数组值向预处理语句传递值 */ $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(); ?>
Use a question mark (?) Parameter to prepare the SQL statement
<?php /* 通过数组值向预处理语句传递值 */ $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(); ?>