PHP Dokumentation: Pdostatement execute
12. Januar 2010 von werner
PDOStatement->execute
(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
PDOStatement->execute — Executes a prepared statement
Beschreibung
Execute the prepared statement. If the prepared statement included parameter markers, you must either:
call PDOStatement::bindParam() to bind PHP variables to the parameter markers: bound variables pass their value as input and receive the output value, if any, of their associated parameter markers
or pass an array of input-only parameter values
Parameter-Liste
- input_parameters
An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR.
You cannot bind multiple values to a single parameter; for example, you cannot bind two values to a single named parameter in an IN() clause.
Rückgabewerte
Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.
Beispiele
Beispiel #1 Execute a prepared statement with bound variables
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>Beispiel #2 Execute a prepared statement with an array of insert values (named parameters)
<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
?>Beispiel #3 Execute a prepared statement with an array of insert values (placeholders)
<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array($calories, $colour));
?>Beispiel #4 Execute a prepared statement with question mark placeholders
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->bindParam(1, $calories, PDO::PARAM_INT);
$sth->bindParam(2, $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>Anmerkungen
Hinweis: Some drivers require to close cursor before executing next statement.
Siehe auch
- PDO::prepare() – Prepares a statement for execution and returns a statement object
- PDOStatement::bindParam() – Binds a parameter to the specified variable name
- PDOStatement::fetch() – Fetches the next row from a result set
- PDOStatement::fetchAll() – Returns an array containing all of the result set rows
- PDOStatement::fetchColumn() – Returns a single column from the next row of a result set