reference/mysqli/mysqli/autocommit.xml
63b99082ef83eade08151f8cb528246fded81db9
...
...
@@ -10,15 +10,15 @@
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::autocommit</methodname>
15
-
<methodparam><type>bool</type><parameter>mode</parameter></methodparam>
13
+
<methodsynopsis role="mysqli">
14
+
<modifier>public</modifier> <type>bool</type><methodname>mysqli::autocommit</methodname>
15
+
<methodparam><type>bool</type><parameter>enable</parameter></methodparam>
16
16
</methodsynopsis>
17
17
<para>&style.procedural;</para>
18
18
<methodsynopsis>
19
19
<type>bool</type><methodname>mysqli_autocommit</methodname>
20
-
<methodparam><type>mysqli</type><parameter>link</parameter></methodparam>
21
-
<methodparam><type>bool</type><parameter>mode</parameter></methodparam>
20
+
<methodparam><type>mysqli</type><parameter>mysql</parameter></methodparam>
21
+
<methodparam><type>bool</type><parameter>enable</parameter></methodparam>
22
22
</methodsynopsis>
23
23
<para>
24
24
Turns on or off auto-commit mode on queries for the database connection.
...
...
@@ -35,7 +35,7 @@
35
35
<variablelist>
36
36
&mysqli.link.description;
37
37
<varlistentry>
38
-
<term><parameter>mode</parameter></term>
38
+
<term><parameter>enable</parameter></term>
39
39
<listitem>
40
40
<para>
41
41
Whether to turn on auto-commit or not.
...
...
@@ -53,14 +53,9 @@
53
53
</para>
54
54
</refsect1>
55
55

56
-
<refsect1 role="notes">
57
-
&reftitle.notes;
58
-
<note>
59
-
<para>
60
-
This function doesn't work with non transactional table types (like
61
-
MyISAM or ISAM).
62
-
</para>
63
-
</note>
56
+
<refsect1 role="errors">
57
+
&reftitle.errors;
58
+
&mysqli.conditionalexception;
64
59
</refsect1>
65
60

66
61
<refsect1 role="examples">
...
...
@@ -71,65 +66,159 @@
71
66
<programlisting role="php">
72
67
<![CDATA[
73
68
<?php
74
-
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
75
69

76
-
if (mysqli_connect_errno()) {
77
-
printf("Connect failed: %s\n", mysqli_connect_error());
78
-
exit();
79
-
}
70
+
/* Tell mysqli to throw an exception if an error occurs */
71
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
80
72

81
-
/* turn autocommit on */
82
-
$mysqli->autocommit(TRUE);
73
+
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
83
74

84
-
if ($result = $mysqli->query("SELECT @@autocommit")) {
75
+
/* The table engine has to support transactions */
76
+
$mysqli->query("CREATE TABLE IF NOT EXISTS language (
77
+
Code text NOT NULL,
78
+
Speakers int(11) NOT NULL
79
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
80
+

81
+
/* Turn autocommit off */
82
+
$mysqli->autocommit(false);
83
+

84
+
$result = $mysqli->query("SELECT @@autocommit");
85
+
$row = $result->fetch_row();
86
+
printf("Autocommit is %s\n", $row[0]);
87
+

88
+
try {
89
+
/* Prepare insert statement */
90
+
$stmt = $mysqli->prepare('INSERT INTO language(Code, Speakers) VALUES (?,?)');
91
+
$stmt->bind_param('ss', $language_code, $native_speakers);
92
+

93
+
/* Insert some values */
94
+
$language_code = 'DE';
95
+
$native_speakers = 50_123_456;
96
+
$stmt->execute();
97
+
$language_code = 'FR';
98
+
$native_speakers = 40_546_321;
99
+
$stmt->execute();
100
+

101
+
/* Commit the data in the database. This doesn't set autocommit=true */
102
+
$mysqli->commit();
103
+
print "Committed 2 rows in the database\n";
104
+

105
+
$result = $mysqli->query("SELECT @@autocommit");
85
106
$row = $result->fetch_row();
86
107
printf("Autocommit is %s\n", $row[0]);
87
-
$result->free();
88
-
}
89
108

90
-
/* close connection */
91
-
$mysqli->close();
92
-
?>
109
+
/* Try to insert more values */
110
+
$language_code = 'PL';
111
+
$native_speakers = 30_555_444;
112
+
$stmt->execute();
113
+
$language_code = 'DK';
114
+
$native_speakers = 5_222_444;
115
+
$stmt->execute();
116
+

117
+
/* Setting autocommit=true will trigger a commit */
118
+
$mysqli->autocommit(true);
119
+

120
+
print "Committed 2 row in the database\n";
121
+
} catch (mysqli_sql_exception $exception) {
122
+
$mysqli->rollback();
123
+

124
+
throw $exception;
125
+
}
93
126
]]>
94
127
</programlisting>
95
128
<para>&style.procedural;</para>
96
129
<programlisting role="php">
97
130
<![CDATA[
98
131
<?php
99
-
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
100
132

101
-
if (!$link) {
102
-
printf("Can't connect to localhost. Error: %s\n", mysqli_connect_error());
103
-
exit();
104
-
}
133
+
/* Tell mysqli to throw an exception if an error occurs */
134
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
135
+

136
+
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");
137
+

138
+
/* The table engine has to support transactions */
139
+
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS language (
140
+
Code text NOT NULL,
141
+
Speakers int(11) NOT NULL
142
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
143
+

144
+
/* Turn autocommit off */
145
+
mysqli_autocommit($mysqli, false);
105
146

106
-
/* turn autocommit on */
107
-
mysqli_autocommit($link, TRUE);
147
+
$result = mysqli_query($mysqli, "SELECT @@autocommit");
148
+
$row = mysqli_fetch_row($result);
149
+
printf("Autocommit is %s\n", $row[0]);
108
150

109
-
if ($result = mysqli_query($link, "SELECT @@autocommit")) {
151
+
try {
152
+
/* Prepare insert statement */
153
+
$stmt = mysqli_prepare($mysqli, 'INSERT INTO language(Code, Speakers) VALUES (?,?)');
154
+
mysqli_stmt_bind_param($stmt, 'ss', $language_code, $native_speakers);
155
+

156
+
/* Insert some values */
157
+
$language_code = 'DE';
158
+
$native_speakers = 50_123_456;
159
+
mysqli_stmt_execute($stmt);
160
+
$language_code = 'FR';
161
+
$native_speakers = 40_546_321;
162
+
mysqli_stmt_execute($stmt);
163
+

164
+
/* Commit the data in the database. This doesn't set autocommit=true */
165
+
mysqli_commit($mysqli);
166
+
print "Committed 2 rows in the database\n";
167
+

168
+
$result = mysqli_query($mysqli, "SELECT @@autocommit");
110
169
$row = mysqli_fetch_row($result);
111
170
printf("Autocommit is %s\n", $row[0]);
112
-
mysqli_free_result($result);
113
-
}
114
171

115
-
/* close connection */
116
-
mysqli_close($link);
117
-
?>
172
+
/* Try to insert more values */
173
+
$language_code = 'PL';
174
+
$native_speakers = 30_555_444;
175
+
mysqli_stmt_execute($stmt);
176
+
$language_code = 'DK';
177
+
$native_speakers = 5_222_444;
178
+
mysqli_stmt_execute($stmt);
179
+

180
+
/* Setting autocommit=true will trigger a commit */
181
+
mysqli_autocommit($mysqli, true);
182
+

183
+
print "Committed 2 row in the database\n";
184
+
} catch (mysqli_sql_exception $exception) {
185
+
mysqli_rollback($mysqli);
186
+

187
+
throw $exception;
188
+
}
118
189
]]>
119
190
</programlisting>
120
191
&examples.outputs;
121
192
<screen>
122
193
<![CDATA[
123
-
Autocommit is 1
194
+
Autocommit is 0
195
+
Committed 2 rows in the database
196
+
Autocommit is 0
197
+
Committed 2 row in the database
198
+
Autocommit is 0
199
+
Committed 2 rows in the database
200
+
Autocommit is 0
201
+
Committed 2 row in the database
124
202
]]>
125
203
</screen>
126
204
</example>
127
205
</refsect1>
128
206

207
+
<refsect1 role="notes">
208
+
&reftitle.notes;
209
+
<note>
210
+
<para>
211
+
This function does not work with non transactional table types (like
212
+
MyISAM or ISAM).
213
+
</para>
214
+
</note>
215
+
</refsect1>
216
+

129
217
<refsect1 role="seealso">
130
218
&reftitle.seealso;
131
219
<para>
132
220
<simplelist>
221
+
<member><function>mysqli_begin_transaction</function></member>
133
222
<member><function>mysqli_commit</function></member>
134
223
<member><function>mysqli_rollback</function></member>
135
224
</simplelist>
...
...
@@ -137,7 +226,6 @@ Autocommit is 1
137
226
</refsect1>
138
227

139
228
</refentry>
140
-

141
229
<!-- Keep this comment at the end of the file
142
230
Local variables:
143
231
mode: sgml
144
232