44 <refnamediv >
55 <refname >mysqli_driver::$report_mode</refname >
66 <refname >mysqli_report</refname >
7- <refpurpose >Enables or disables internal report functions </refpurpose >
7+ <refpurpose >Sets mysqli error reporting mode </refpurpose >
88 </refnamediv >
99
1010 <refsect1 role =" description" >
1919 <methodparam ><type >int</type ><parameter >flags</parameter ></methodparam >
2020 </methodsynopsis >
2121 <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 >.
2528 </para >
2629 </refsect1 >
2730
7982 <refsect1 role =" returnvalues" >
8083 &reftitle.returnvalues;
8184 <para >
82- &return.success;
85+ Returns &true; .
8386 </para >
8487 </refsect1 >
8588
9194<![CDATA[
9295<?php
9396
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-
10297/* activate reporting */
10398$driver = new mysqli_driver();
10499$driver->report_mode = MYSQLI_REPORT_ALL;
105100
106101try {
102+ /* if the connection fails, a mysqli_sql_exception will be thrown */
103+ $mysqli = new mysqli("localhost", "my_user", "my_password", "my_db");
107104
108105 /* this query should report an error */
109106 $result = $mysqli->query("SELECT Name FROM Nonexistingtable WHERE population > 50000");
110107
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 */
112109 $result = $mysqli->query("SELECT Name FROM City WHERE population > 50000");
113-
114- $result->close();
115-
116- $mysqli->close();
117-
118110} catch (mysqli_sql_exception $e) {
119-
120- echo $e->__toString();
111+ error_log($e->__toString());
121112}
122- ?>
123113]]>
124114 </programlisting >
125115 </example >
@@ -128,27 +118,45 @@ try {
128118 <programlisting role =" php" >
129119<![CDATA[
130120<?php
121+
131122/* activate reporting */
132123mysqli_report(MYSQLI_REPORT_ALL);
133124
134- $link = mysqli_connect("localhost", "my_user", "my_password", "world");
125+ try {
126+ $link = mysqli_connect("localhost", "my_user", "my_password", "my_db");
135127
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());
140135}
136+ ]]>
137+ </programlisting >
138+ </example >
139+ <example >
140+ <title >Error reporting except bad index errors</title >
141+ <programlisting role =" php" >
142+ <![CDATA[
143+ <?php
141144
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 );
144147
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");
147151
148- mysqli_free_result($result);
152+ /* this query should report an error */
153+ $result = $mysqli->query("SELECT Name FROM Nonexistingtable WHERE population > 50000");
149154
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+ }
152160]]>
153161 </programlisting >
154162 </example >
@@ -158,8 +166,6 @@ mysqli_close($link);
158166 &reftitle.seealso;
159167 <para >
160168 <simplelist >
161- <member ><function >mysqli_debug</function ></member >
162- <member ><function >mysqli_dump_debug_info</function ></member >
163169 <member ><classname >mysqli_sql_exception</classname ></member >
164170 <member ><function >set_exception_handler</function ></member >
165171 <member ><function >error_reporting</function ></member >
0 commit comments