php-doc-en/reference/pdo/pdostatement/execute.xml
2014-09-05 07:44:07 +00:00

228 lines
6.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="pdostatement.execute" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>PDOStatement::execute</refname>
<refpurpose>
Executes a prepared statement
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>bool</type><methodname>PDOStatement::execute</methodname>
<methodparam choice="opt"><type>array</type><parameter>input_parameters</parameter></methodparam>
</methodsynopsis>
<para>
Execute the prepared statement. If the prepared statement included
parameter markers, you must either:
<itemizedlist>
<listitem><para>call <function>PDOStatement::bindParam</function> 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</para></listitem>
<listitem><para>or pass an array of input-only parameter values</para></listitem>
</itemizedlist>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>input_parameters</parameter></term>
<listitem>
<para>
An array of values with as many elements as there are bound
parameters in the SQL statement being executed.
All values are treated as <constant>PDO::PARAM_STR</constant>.
</para>
<para>
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.
</para>
<para>
You cannot bind more values than specified; if more keys exist in
<parameter>input_parameters</parameter> than in the SQL specified
in the <methodname>PDO::prepare</methodname>, then the statement will
fail and an error is emitted.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.success;
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>5.2.0</entry>
<entry>
The keys from <parameter>input_parameters</parameter> must match the ones
declared in the SQL. Before PHP 5.2.0 this was silently ignored.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example><title>Execute a prepared statement with bound variables</title>
<programlisting role="php">
<![CDATA[
<?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();
?>
]]>
</programlisting>
</example>
<example><title>Execute a prepared statement with an array of insert values (named parameters)</title>
<programlisting role="php">
<![CDATA[
<?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));
?>
]]>
</programlisting>
</example>
<example><title>Execute a prepared statement with an array of insert values (placeholders)</title>
<programlisting role="php">
<![CDATA[
<?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));
?>
]]>
</programlisting>
</example>
<example><title>Execute a prepared statement with question mark placeholders</title>
<programlisting role="php">
<![CDATA[
<?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();
?>
]]>
</programlisting>
</example>
<example><title>Execute a prepared statement using array for IN clause</title>
<programlisting role="php">
<![CDATA[
<?php
/* Execute a prepared statement using an array of values for an IN clause */
$params = array(1, 21, 63, 171);
/* Create a string for the parameter placeholders filled to the number of params */
$place_holders = implode(',', array_fill(0, count($params), '?'));
/*
This prepares the statement with enough unnamed placeholders for every value
in our $params array. The values of the $params array are then bound to the
placeholders in the prepared statement when the statement is executed.
This is not the same thing as using PDOStatement::bindParam() since this
requires a reference to the variable. PDOStatement::execute() only binds
by value instead.
*/
$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");
$sth->execute($params);
?>
]]>
</programlisting>
</example>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
Some drivers require to <link linkend="pdostatement.closecursor">close
cursor</link> before executing next statement.
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>PDO::prepare</function></member>
<member><function>PDOStatement::bindParam</function></member>
<member><function>PDOStatement::fetch</function></member>
<member><function>PDOStatement::fetchAll</function></member>
<member><function>PDOStatement::fetchColumn</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->