PDOStatement::execute

(no version information, might be only in CVS)

PDOStatement::execute --  Executes a prepared statement

说明

bool PDOStatement::execute ( [array input_parameters] )

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

范例

例子 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();
?>

例子 2. Execute a prepared statement with an array of insert values

<?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));
?>

例子 3. 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();
?>

参见

PDO::prepare()
PDOStatement::bindParam()
PDOStatement::fetch()
PDOStatement::fetchAll()
PDOStatement::fetchColumn()


add a note add a note User Contributed Notes
inghamn at bloomington dot in dot gov
25-Oct-2006 04:53
Just a note, I'm currently using 5.1.6.  After debugging for a good amount of time, I've realized that the $array cannot be an associative array.

For instance, the following code will fail on the execute.
<?php
   $fields
= array();
  
$fields['name'] = "Someone";

  
$query = $PDO->prepare("insert table set name=?");
  
$query->execute($fields);
?>

For my code, I had to convert the $fields array into a non-associative array.  Something like this:
<?php
   $fields
= array();
  
$fields['name'] = "Someone";

  
$values = array();
   foreach(
$fields as $value) { $values[] = $value; }

  
$query = $PDO->prepare("insert table set name=?");
  
$query->execute($values);
?>
russel at sunraystudios dot com
16-Oct-2006 05:42
I've used it and it returns booleans=>
$passed = $stmt->execute();
if($passed){
echo "passed";
} else {
echo "failed";
}

If the statement failed it would print failed.  You would want to use errorInfo() to get more info, but it does seem to work for me.
Nei
07-Feb-2006 03:40
This does not return TRUE or FALSE. It seems to just not return anything.