PHP: PDOStatement->execute() - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

PDOStatement->fetch() PDOStatement->errorInfo()

Last updated: Sat, 27 Jan 2007

view this page in

# PDOStatement->execute()

(no version information, might be only in CVS)

PDOStatement->execute() -- Executes a prepared statement

## Описание

class PDOStatement {
bool 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

## Список параметров

`input_parameters`

  An array of values with as many elements as there are bound parameters in the SQL statement being executed.

  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.

## Возвращаемые значения

Возвращает `TRUE` в случае успешного завершения или `FALSE` в случае возникновения ошибки.

## Примеры

Пример 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 (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));
?>`

Пример 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));
?>`

Пример 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();
?>`

## Смотрите также

PDO->prepare()
PDOStatement->bindParam()
PDOStatement->fetch()
PDOStatement->fetchAll()
PDOStatement->fetchColumn()

add a note User Contributed Notes
PDOStatement->execute()
narcis at narcisradu dot com
08-Jan-2007 01:16
`For a query like this:
SELECT
t1.user_id, t1.user_name,
t2.*
FROM table1 t1
LEFT JOIN table2 t2 ON t2.user_id = t1.user_id
WHERE t1.user_id = 2
If I don't have an entry in table2 for user_id=2, the user_id in result will be empty.
SELECT
t1.user_id, t1.user_name,
t2.user_pet, t2.user_color, t2.user_sign
FROM table1 t1
LEFT JOIN table2 t2 ON t2.user_id = t1.user_id
WHERE t1.user_id = 2
This query will return nonempty user_id.
So please be careful with wildcard select.`
VolGas
22-Dec-2006 10:38
`An array of insert values (named parameters) don't need the prefixed colon als key-value to work.
<?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');
// instead of:
// $sth->execute(array(':calories' => $calories, ':colour' => $colour));
// this works fine, too:
$sth->execute(array('calories' => $calories, 'colour' => $colour));
?>
This allows to use "regular" assembled hash-tables (arrays).
That realy does make sense!`
dbrucas
21-Nov-2006 10:54
`If you don't want to turn on exception raising, then try this:
//$dbErr = $dbHandler->errorInfo(); OR
$dbErr = $dbStatement->errorInfo();
if ( $dbErr[0] != '00000' ) {
print_r($dbHandler->errorInfo());
die( "<div class='redbg xlarge'>FAILED: $msg</div><br />".$foot);
// or handle the error your way...
}
echo "SUCCESS: $msg<br />";
... continue if succesful`
inghamn at bloomington dot in dot gov
24-Oct-2006 01: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
15-Oct-2006 02: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
06-Feb-2006 11:40
`This does not return TRUE or FALSE. It seems to just not return anything.`
add a note
PDOStatement->fetch() PDOStatement->errorInfo()

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST