Easy Tutorial
❮ Php Error Pdostatement Setfetchmode ❯

PDOStatement::execute

PHP PDO Reference Manual

PDOStatement::execute — Executes a prepared statement (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)


Description

Syntax

bool PDOStatement::execute ([ array $input_parameters ] )

Executes a prepared statement. If the prepared statement includes parameter markers, you must either:


Parameters

input_parameters

An array with the same number of elements as the number of 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.

The bound values cannot exceed the specified number. If there are more keys in input_parameters than in the SQL statement specified in PDO::prepare(), the statement will fail and an error will be issued.


Return Value

Returns TRUE on success or FALSE on failure.


Examples

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

Executing a prepared statement with an array of input 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));
?>

Executing a prepared statement with an array of input 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));
?>

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

Executing a prepared statement with an IN clause using an array

<?php
/* Execute a prepared statement with an IN clause using an array of values */
$params = array(1, 21, 63, 171);
/* Create a string filled with placeholders with the same number as params */
$place_holders = implode(',', array_fill(0, count($params), '?'));

/*
    For each value in the $params array, the prepared statement contains enough unnamed placeholders.
    When the statement is executed, the values in the $params array are bound to the placeholders in the prepared statement.

This is different from using PDOStatement::bindParam() because it requires a reference variable. PDOStatement::execute() serves as an alternative for binding by value.

$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");
$sth->execute($params);
?>

PHP PDO Reference Manual

❮ Php Error Pdostatement Setfetchmode ❯