reference/mysqli/mysqli_stmt/execute.xml
ec946465e5ee9dc4dc19f1a28510697066a19eac
...
...
@@ -4,41 +4,38 @@
4
4
<refnamediv>
5
5
<refname>mysqli_stmt::execute</refname>
6
6
<refname>mysqli_stmt_execute</refname>
7
-
<refpurpose>Executes a prepared Query</refpurpose>
7
+
<refpurpose>Executes a prepared statement</refpurpose>
8
8
</refnamediv>
9
9

10
10
<refsect1 role="description">
11
11
&reftitle.description;
12
12
<para>&style.oop;</para>
13
-
<methodsynopsis role="oop">
14
-
<type>bool</type><methodname>mysqli_stmt::execute</methodname>
15
-
<void />
13
+
<methodsynopsis role="mysqli_stmt">
14
+
<modifier>public</modifier> <type>bool</type><methodname>mysqli_stmt::execute</methodname>
15
+
<methodparam choice="opt"><type class="union"><type>array</type><type>null</type></type><parameter>params</parameter><initializer>&null;</initializer></methodparam>
16
16
</methodsynopsis>
17
17
<para>&style.procedural;</para>
18
18
<methodsynopsis>
19
19
<type>bool</type><methodname>mysqli_stmt_execute</methodname>
20
-
<methodparam><type>mysqli_stmt</type><parameter>stmt</parameter></methodparam>
20
+
<methodparam><type>mysqli_stmt</type><parameter>statement</parameter></methodparam>
21
+
<methodparam choice="opt"><type class="union"><type>array</type><type>null</type></type><parameter>params</parameter><initializer>&null;</initializer></methodparam>
21
22
</methodsynopsis>
22
23
<para>
23
-
Executes a query that has been previously prepared using the
24
-
<function>mysqli_prepare</function> function. When executed any
25
-
parameter markers which exist will automatically be replaced with the
26
-
appropriate data.
24
+
Executes previously prepared statement. The statement must be successfully
25
+
prepared prior to execution, using either
26
+
the <function>mysqli_prepare</function> or
27
+
<function>mysqli_stmt_prepare</function> function, or by passing the second
28
+
argument to <methodname>mysqli_stmt::__construct</methodname>.
27
29
</para>
28
30
<para>
29
31
If the statement is <literal>UPDATE</literal>, <literal>DELETE</literal>,
30
32
or <literal>INSERT</literal>, the total number of affected rows can be
31
33
determined by using the <function>mysqli_stmt_affected_rows</function>
32
-
function. Likewise, if the query yields a result set the
33
-
<function>mysqli_stmt_fetch</function> function is used.
34
+
function. If the query yields a result set, it can be fetched using
35
+
<function>mysqli_stmt_get_result</function> function or by fetching it
36
+
row by row directly from the statement using
37
+
<function>mysqli_stmt_fetch</function> function.
34
38
</para>
35
-
<note>
36
-
<para>
37
-
When using <function>mysqli_stmt_execute</function>, the
38
-
<function>mysqli_stmt_fetch</function> function must be used to fetch the
39
-
data prior to performing any additional queries.
40
-
</para>
41
-
</note>
42
39
</refsect1>
43
40

44
41
<refsect1 role="parameters">
...
...
@@ -46,6 +43,14 @@
46
43
<para>
47
44
<variablelist>
48
45
&mysqli.stmt.description;
46
+
<varlistentry>
47
+
<term><parameter>params</parameter></term>
48
+
<listitem>
49
+
<para>
50
+
An optional list &array; with as many elements as there are bound parameters in the SQL statement being executed. Each value is treated as a &string;.
51
+
</para>
52
+
</listitem>
53
+
</varlistentry>
49
54
</variablelist>
50
55
</para>
51
56
</refsect1>
...
...
@@ -57,27 +62,51 @@
57
62
</para>
58
63
</refsect1>
59
64

65
+
<refsect1 role="errors">
66
+
&reftitle.errors;
67
+
&mysqli.conditionalexception;
68
+
</refsect1>
69
+

70
+
<refsect1 role="changelog">
71
+
&reftitle.changelog;
72
+
<informaltable>
73
+
<tgroup cols="2">
74
+
<thead>
75
+
<row>
76
+
<entry>&Version;</entry>
77
+
<entry>&Description;</entry>
78
+
</row>
79
+
</thead>
80
+
<tbody>
81
+
<row>
82
+
<entry>8.1.0</entry>
83
+
<entry>
84
+
The optional <parameter>params</parameter> parameter has been added.
85
+
</entry>
86
+
</row>
87
+
</tbody>
88
+
</tgroup>
89
+
</informaltable>
90
+
</refsect1>
91
+

60
92
<refsect1 role="examples">
61
93
&reftitle.examples;
62
94
<example>
63
-
<title>&style.oop;</title>
95
+
<title>Execute a prepared statement with bound variables</title>
96
+
<para>&style.oop;</para>
64
97
<programlisting role="php">
65
98
<![CDATA[
66
99
<?php
67
-
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
68
100

69
-
/* check connection */
70
-
if (mysqli_connect_errno()) {
71
-
printf("Connect failed: %s\n", mysqli_connect_error());
72
-
exit();
73
-
}
101
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
102
+
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
74
103

75
-
$mysqli->query("CREATE TABLE myCity LIKE City");
104
+
$mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City");
76
105

77
106
/* Prepare an insert statement */
78
-
$query = "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)";
79
-
$stmt = $mysqli->prepare($query);
107
+
$stmt = $mysqli->prepare("INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)");
80
108

109
+
/* Bind variables to parameters */
81
110
$stmt->bind_param("sss", $val1, $val2, $val3);
82
111

83
112
$val1 = 'Stuttgart';
...
...
@@ -94,47 +123,28 @@ $val3 = 'Aquitaine';
94
123
/* Execute the statement */
95
124
$stmt->execute();
96
125

97
-
/* close statement */
98
-
$stmt->close();
99
-

100
-
/* retrieve all rows from myCity */
126
+
/* Retrieve all rows from myCity */
101
127
$query = "SELECT Name, CountryCode, District FROM myCity";
102
-
if ($result = $mysqli->query($query)) {
103
-
while ($row = $result->fetch_row()) {
104
-
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
105
-
}
106
-
/* free result set */
107
-
$result->close();
128
+
$result = $mysqli->query($query);
129
+
while ($row = $result->fetch_row()) {
130
+
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
108
131
}
109
-

110
-
/* remove table */
111
-
$mysqli->query("DROP TABLE myCity");
112
-

113
-
/* close connection */
114
-
$mysqli->close();
115
-
?>
116
132
]]>
117
133
</programlisting>
118
-
</example>
119
-
<example>
120
-
<title>&style.procedural;</title>
134
+
<para>&style.procedural;</para>
121
135
<programlisting role="php">
122
136
<![CDATA[
123
137
<?php
124
-
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
125
138

126
-
/* check connection */
127
-
if (mysqli_connect_errno()) {
128
-
printf("Connect failed: %s\n", mysqli_connect_error());
129
-
exit();
130
-
}
139
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
140
+
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
131
141

132
-
mysqli_query($link, "CREATE TABLE myCity LIKE City");
142
+
mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City");
133
143

134
144
/* Prepare an insert statement */
135
-
$query = "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)";
136
-
$stmt = mysqli_prepare($link, $query);
145
+
$stmt = mysqli_prepare($link, "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)");
137
146

147
+
/* Bind variables to parameters */
138
148
mysqli_stmt_bind_param($stmt, "sss", $val1, $val2, $val3);
139
149

140
150
$val1 = 'Stuttgart';
...
...
@@ -151,25 +161,12 @@ $val3 = 'Aquitaine';
151
161
/* Execute the statement */
152
162
mysqli_stmt_execute($stmt);
153
163

154
-
/* close statement */
155
-
mysqli_stmt_close($stmt);
156
-

157
-
/* retrieve all rows from myCity */
164
+
/* Retrieve all rows from myCity */
158
165
$query = "SELECT Name, CountryCode, District FROM myCity";
159
-
if ($result = mysqli_query($link, $query)) {
160
-
while ($row = mysqli_fetch_row($result)) {
161
-
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
162
-
}
163
-
/* free result set */
164
-
mysqli_free_result($result);
166
+
$result = mysqli_query($link, $query);
167
+
while ($row = mysqli_fetch_row($result)) {
168
+
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
165
169
}
166
-

167
-
/* remove table */
168
-
mysqli_query($link, "DROP TABLE myCity");
169
-

170
-
/* close connection */
171
-
mysqli_close($link);
172
-
?>
173
170
]]>
174
171
</programlisting>
175
172
&examples.outputs;
...
...
@@ -180,12 +177,70 @@ Bordeaux (FRA,Aquitaine)
180
177
]]>
181
178
</screen>
182
179
</example>
180
+
<example>
181
+
<title>Execute a prepared statement with an array of values</title>
182
+
<para>&style.oop;</para>
183
+
<programlisting role="php">
184
+
<![CDATA[
185
+
<?php
186
+

187
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
188
+
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
189
+

190
+
$mysqli->query('CREATE TEMPORARY TABLE myCity LIKE City');
191
+

192
+
/* Prepare an insert statement */
193
+
$stmt = $mysqli->prepare('INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)');
194
+

195
+
/* Execute the statement */
196
+
$stmt->execute(['Stuttgart', 'DEU', 'Baden-Wuerttemberg']);
197
+

198
+
/* Retrieve all rows from myCity */
199
+
$query = 'SELECT Name, CountryCode, District FROM myCity';
200
+
$result = $mysqli->query($query);
201
+
while ($row = $result->fetch_row()) {
202
+
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
203
+
}
204
+
]]>
205
+
</programlisting>
206
+
<para>&style.procedural;</para>
207
+
<programlisting role="php">
208
+
<![CDATA[
209
+
<?php
210
+

211
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
212
+
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');
213
+

214
+
mysqli_query($link, 'CREATE TEMPORARY TABLE myCity LIKE City');
215
+

216
+
/* Prepare an insert statement */
217
+
$stmt = mysqli_prepare($link, 'INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)');
218
+

219
+
/* Execute the statement */
220
+
mysqli_stmt_execute($stmt, ['Stuttgart', 'DEU', 'Baden-Wuerttemberg']);
221
+

222
+
/* Retrieve all rows from myCity */
223
+
$query = 'SELECT Name, CountryCode, District FROM myCity';
224
+
$result = mysqli_query($link, $query);
225
+
while ($row = mysqli_fetch_row($result)) {
226
+
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
227
+
}
228
+
]]>
229
+
</programlisting>
230
+
&examples.outputs;
231
+
<screen>
232
+
<![CDATA[
233
+
Stuttgart (DEU,Baden-Wuerttemberg)
234
+
]]>
235
+
</screen>
236
+
</example>
183
237
</refsect1>
184
238

185
239
<refsect1 role="seealso">
186
240
&reftitle.seealso;
187
241
<para>
188
242
<simplelist>
243
+
<member><function>mysqli_execute_query</function></member>
189
244
<member><function>mysqli_prepare</function></member>
190
245
<member><function>mysqli_stmt_bind_param</function></member>
191
246
<member><function>mysqli_stmt_get_result</function></member>
...
...
@@ -194,7 +249,6 @@ Bordeaux (FRA,Aquitaine)
194
249
</refsect1>
195
250

196
251
</refentry>
197
-

198
252
<!-- Keep this comment at the end of the file
199
253
Local variables:
200
254
mode: sgml
201
255