There are two great examples on the pdo::prepare() documentation.
I have included them here and simplified them a bit.
This one uses ?
parameters. $dbh
is basically a PDO object. And what you are doing is putting the values 150
and 'red'
into the first and second question mark respectively.
/* Execute a prepared statement by passing an array of values */
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array(150, 'red'));
$red = $sth->fetchAll();
This one uses named parameters and is a bit more complex.
/* Execute a prepared statement by passing an array of values */
$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();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…