4
4
<refnamediv >
5
5
<refname >mysqli_driver::$report_mode</refname >
6
6
<refname >mysqli_report</refname >
7
- <refpurpose >Enables or disables internal report functions </refpurpose >
7
+ <refpurpose >Sets mysqli error reporting mode </refpurpose >
8
8
</refnamediv >
9
9
10
10
<refsect1 role =" description" >
19
19
<methodparam ><type >int</type ><parameter >flags</parameter ></methodparam >
20
20
</methodsynopsis >
21
21
<para >
22
- A function helpful in improving queries during code development and testing.
23
- Depending on the flags, it reports errors from mysqli function calls or
24
- queries that don't use an index (or use a bad index).
22
+ Depending on the flags, it sets mysqli error reporting mode to exception, warning or none.
23
+ When set to <constant >MYSQLI_REPORT_ALL</constant > or <constant >MYSQLI_REPORT_INDEX</constant >
24
+ it will also inform about queries that don't use an index (or use a bad index).
25
+ </para >
26
+ <para >
27
+ The default setting is <constant >MYSQLI_REPORT_OFF</constant >.
25
28
</para >
26
29
</refsect1 >
27
30
79
82
<refsect1 role =" returnvalues" >
80
83
&reftitle.returnvalues;
81
84
<para >
82
- &return.success;
85
+ Returns &true; .
83
86
</para >
84
87
</refsect1 >
85
88
91
94
<![CDATA[
92
95
<?php
93
96
94
- $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
95
-
96
- /* check connection */
97
- if (mysqli_connect_errno()) {
98
- printf("Connect failed: %s\n", mysqli_connect_error());
99
- exit();
100
- }
101
-
102
97
/* activate reporting */
103
98
$driver = new mysqli_driver();
104
99
$driver->report_mode = MYSQLI_REPORT_ALL;
105
100
106
101
try {
102
+ /* if the connection fails, a mysqli_sql_exception will be thrown */
103
+ $mysqli = new mysqli("localhost", "my_user", "my_password", "my_db");
107
104
108
105
/* this query should report an error */
109
106
$result = $mysqli->query("SELECT Name FROM Nonexistingtable WHERE population > 50000");
110
107
111
- /* this query should report a bad index */
108
+ /* this query should report a bad index if the column population doesn't have an index */
112
109
$result = $mysqli->query("SELECT Name FROM City WHERE population > 50000");
113
-
114
- $result->close();
115
-
116
- $mysqli->close();
117
-
118
110
} catch (mysqli_sql_exception $e) {
119
-
120
- echo $e->__toString();
111
+ error_log($e->__toString());
121
112
}
122
- ?>
123
113
]]>
124
114
</programlisting >
125
115
</example >
@@ -128,27 +118,45 @@ try {
128
118
<programlisting role =" php" >
129
119
<![CDATA[
130
120
<?php
121
+
131
122
/* activate reporting */
132
123
mysqli_report(MYSQLI_REPORT_ALL);
133
124
134
- $link = mysqli_connect("localhost", "my_user", "my_password", "world");
125
+ try {
126
+ $link = mysqli_connect("localhost", "my_user", "my_password", "my_db");
135
127
136
- /* check connection */
137
- if (mysqli_connect_errno()) {
138
- printf("Connect failed: %s\n", mysqli_connect_error());
139
- exit();
128
+ /* this query should report an error */
129
+ $result = mysqli_query($link, "SELECT Name FROM Nonexistingtable WHERE population > 50000");
130
+
131
+ /* this query should report a bad index if the column population doesn't have an index */
132
+ $result = mysqli_query($link, "SELECT Name FROM City WHERE population > 50000");
133
+ } catch (mysqli_sql_exception $e) {
134
+ error_log($e->__toString());
140
135
}
136
+ ]]>
137
+ </programlisting >
138
+ </example >
139
+ <example >
140
+ <title >Error reporting except bad index errors</title >
141
+ <programlisting role =" php" >
142
+ <![CDATA[
143
+ <?php
141
144
142
- /* this query should report an error */
143
- $result = mysqli_query("SELECT Name FROM Nonexistingtable WHERE population > 50000" );
145
+ /* activate reporting */
146
+ mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
144
147
145
- /* this query should report a bad index */
146
- $result = mysqli_query("SELECT Name FROM City WHERE population > 50000");
148
+ try {
149
+ /* if the connection fails, a mysqli_sql_exception will be thrown */
150
+ $mysqli = new mysqli("localhost", "my_user", "my_password", "my_db");
147
151
148
- mysqli_free_result($result);
152
+ /* this query should report an error */
153
+ $result = $mysqli->query("SELECT Name FROM Nonexistingtable WHERE population > 50000");
149
154
150
- mysqli_close($link);
151
- ?>
155
+ /* this WILL NOT report any errors even if index is not available */
156
+ $result = $mysqli->query("SELECT Name FROM City WHERE population > 50000");
157
+ } catch (mysqli_sql_exception $e) {
158
+ error_log($e->__toString());
159
+ }
152
160
]]>
153
161
</programlisting >
154
162
</example >
@@ -158,8 +166,6 @@ mysqli_close($link);
158
166
&reftitle.seealso;
159
167
<para >
160
168
<simplelist >
161
- <member ><function >mysqli_debug</function ></member >
162
- <member ><function >mysqli_dump_debug_info</function ></member >
163
169
<member ><classname >mysqli_sql_exception</classname ></member >
164
170
<member ><function >set_exception_handler</function ></member >
165
171
<member ><function >error_reporting</function ></member >
0 commit comments