1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
<?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 role="PDOStatement">
<modifier>public</modifier> <type>bool</type><methodname>PDOStatement::execute</methodname>
<methodparam choice="opt"><type class="union"><type>array</type><type>null</type></type><parameter>params</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><methodname>PDOStatement::bindParam</methodname> and/or
<methodname>PDOStatement::bindValue</methodname> 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>params</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>params</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="errors">
&reftitle.errors;
&pdo.errors;
</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);
/* Names can be prefixed with colons ":" too (optional) */
$sth->bindValue(':colour', "%$colour%");
$sth->execute();
?>
]]>
</programlisting>
</example>
<example><title>Execute a prepared statement with an array of named values</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));
/* Array keys can be prefixed with colons ":" too (optional) */
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
?>
]]>
</programlisting>
</example>
<example><title>Execute a prepared statement with an array of positional values</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 variables bound to positional 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 = '?' . str_repeat(', ?', count($params) - 1);
/*
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><methodname>PDO::prepare</methodname></member>
<member><methodname>PDOStatement::bindParam</methodname></member>
<member><methodname>PDOStatement::fetch</methodname></member>
<member><methodname>PDOStatement::fetchAll</methodname></member>
<member><methodname>PDOStatement::fetchColumn</methodname></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
-->
|