MySQLi Tutorial MySQLi References

mysqli_driver $report_mode Property



The mysqli_driver::$report_mode / mysqli_report() function , depending on the flags, sets mysqli error reporting mode to exception, warning or none. When set to MYSQLI_REPORT_ALL or MYSQLI_REPORT_INDEX it will also inform about queries that does not use an index (or use a bad index).

Syntax

//Object-oriented style
$mysqli_driver->report_mode;

//Procedural style
mysqli_report(flags)

Parameters

flags Required. Specify flags. It can take following values:
  • MYSQLI_REPORT_OFF - Turns reporting off
  • MYSQLI_REPORT_ERROR - Report errors from mysqli function calls
  • MYSQLI_REPORT_STRICT - Throw mysqli_sql_exception for errors instead of warnings
  • MYSQLI_REPORT_INDEX - Report if no index or bad index was used in a query
  • MYSQLI_REPORT_ALL - Set all options (report all)
As of PHP 8.1.0, the default setting is MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT. Previously, it was MYSQLI_REPORT_OFF.

Return Value

Returns true.

Example: Object-oriented style

The example below shows the usage of mysqli_driver::$report_mode property.

<?php
//activate reporting
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ALL;

try {
  //establishing connection to the database, if the connection 
  //fails, a mysqli_sql_exception will be thrown
  $mysqli = new mysqli("localhost", "user", "password", "database");

  //getting query result from the database
  $sql = "SELECT Name, Age FROM Employee ORDER BY Age";
  $result = $mysqli->query($sql);

  //processing the data retrieved from the database
  //- fetching all result rows as associative array
  $rows = $result->fetch_all(MYSQLI_ASSOC);

  //displaying the rows
  foreach ($rows as $row) {
    printf("%s, %d\n", $row["Name"], $row["Age"]);
  }
} catch (mysqli_sql_exception $e) {
  error_log($e->__toString());
}
?>

The output of the above code will be similar to:

Marry, 23
Kim, 26
John, 27
Adam, 28

Example: Procedural style

The example below shows the usage of mysqli_report() function.

<?php
//activate reporting
mysqli_report(MYSQLI_REPORT_ALL);

try {
  //establishing connection to the database
  $mysqli = mysqli_connect("localhost", "user", "password", "database");

  //getting query result from the database
  $sql = "SELECT Name, Age FROM Employee ORDER BY Age";
  $result = mysqli_query($mysqli, $sql);

  //processing the data retrieved from the database
  //- fetching all result rows as associative array
  $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

  //displaying the rows
  foreach ($rows as $row) {
    printf("%s, %d\n", $row["Name"], $row["Age"]);
  }
} catch (mysqli_sql_exception $e) {
  error_log($e->__toString());
}
?>

The output of the above code will be similar to:

Marry, 23
Kim, 26
John, 27
Adam, 28

Example: Error reporting except bad index errors

The example below shows how to use this function for all error reporting except bad index errors.

<?php
//activate reporting
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

try {
  //establishing connection to the database
  $mysqli = mysqli_connect("localhost", "user", "password", "database");

  //getting query result from the database
  $sql = "SELECT Name, Age FROM Employee ORDER BY Age";
  $result = mysqli_query($mysqli, $sql);

  //processing the data retrieved from the database
  //- fetching all result rows as associative array
  $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

  //displaying the rows
  foreach ($rows as $row) {
    printf("%s, %d\n", $row["Name"], $row["Age"]);
  }
} catch (mysqli_sql_exception $e) {
  error_log($e->__toString());
}
?>

The output of the above code will be similar to:

Marry, 23
Kim, 26
John, 27
Adam, 28

❮ MySQLi Functions Reference