php-doc-en/reference/pdo/pdostatement/execute.xml
Christoph Michael Becker 9af43469f4 Remove more changelog entries, mostly from PHP 5.2 era
Patch contributed by Sobak <msobaczewski@gmail.com>.


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@350654 c90b9560-bf6c-de11-be94-00142212c4b1
2020-09-26 13:52:31 +00:00

207 lines
6.3 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><initializer>&null;</initializer></methodparam>
</methodsynopsis>
<para>
Execute the <link linkend="pdo.prepared-statements">prepared
statement</link>. If the prepared statement included parameter markers, either:
<itemizedlist>
<listitem><para><function>PDOStatement::bindParam</function> and/or
<function>PDOStatement::bindValue</function> has to be called to bind either variables or
values (respectively) 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 an array of input-only parameter values has to be passed</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>
Multiple values cannot be bound to a single parameter; for example,
it is not allowed to bind two values to a single named parameter in an IN()
clause.
</para>
<para>
Binding more values than specified is not possible; 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="examples">
&reftitle.examples;
<example><title>Execute a prepared statement with a bound variable and value</title>
<programlisting role="php">
<![CDATA[
<?php
/* Execute a prepared statement by binding a variable and value */
$calories = 150;
$colour = 'gre';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour LIKE :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindValue(':colour', "%{$colour}%");
$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
-->