reference/mysqli/mysqli/autocommit.xml
63b99082ef83eade08151f8cb528246fded81db9
...
...
@@ -4,21 +4,21 @@
4
4
<refnamediv>
5
5
<refname>mysqli::autocommit</refname>
6
6
<refname>mysqli_autocommit</refname>
7
-
<refpurpose>Turns on or off auto-commiting database modifications</refpurpose>
7
+
<refpurpose>Turns on or off auto-committing database modifications</refpurpose>
8
8
</refnamediv>
9
9

10
10
<refsect1 role="description">
11
11
&reftitle.description;
12
-
<para>Object oriented style (method)</para>
13
-
<methodsynopsis>
14
-
<type>bool</type><methodname>mysqli::autocommit</methodname>
15
-
<methodparam><type>bool</type><parameter>mode</parameter></methodparam>
16
-
</methodsynopsis>
17
-
<para>Procedural style:</para>
12
+
<para>&style.oop;</para>
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
+
</methodsynopsis>
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,84 +53,172 @@
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">
67
62
&reftitle.examples;
68
63
<example>
69
-
<title>Object oriented style</title>
64
+
<title><methodname>mysqli::autocommit</methodname> example</title>
65
+
<para>&style.oop;</para>
70
66
<programlisting role="php">
71
67
<![CDATA[
72
68
<?php
73
-
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
74
69

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

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

83
-
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");
84
106
$row = $result->fetch_row();
85
107
printf("Autocommit is %s\n", $row[0]);
86
-
$result->free();
87
-
}
88
108

89
-
/* close connection */
90
-
$mysqli->close();
91
-
?>
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
+
}
92
126
]]>
93
127
</programlisting>
94
-
</example>
95
-
<example>
96
-
<title>Procedural style</title>
128
+
<para>&style.procedural;</para>
97
129
<programlisting role="php">
98
130
<![CDATA[
99
131
<?php
100
-
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
101
132

102
-
if (!$link) {
103
-
printf("Can't connect to localhost. Error: %s\n", mysqli_connect_error());
104
-
exit();
105
-
}
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;");
106
143

107
-
/* turn autocommit on */
108
-
mysqli_autocommit($link, TRUE);
144
+
/* Turn autocommit off */
145
+
mysqli_autocommit($mysqli, false);
109
146

110
-
if ($result = mysqli_query($link, "SELECT @@autocommit")) {
147
+
$result = mysqli_query($mysqli, "SELECT @@autocommit");
148
+
$row = mysqli_fetch_row($result);
149
+
printf("Autocommit is %s\n", $row[0]);
150
+

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");
111
169
$row = mysqli_fetch_row($result);
112
170
printf("Autocommit is %s\n", $row[0]);
113
-
mysqli_free_result($result);
114
-
}
115
171

116
-
/* close connection */
117
-
mysqli_close($link);
118
-
?>
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
+
}
119
189
]]>
120
190
</programlisting>
121
-
</example>
122
-
&example.outputs;
123
-
<screen>
191
+
&examples.outputs;
192
+
<screen>
124
193
<![CDATA[
125
-
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
126
202
]]>
127
-
</screen>
203
+
</screen>
204
+
</example>
205
+
</refsect1>
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>
128
215
</refsect1>
129
216

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

140
228
</refentry>
141
-

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