mysqli_driver::$report_mode

mysqli_report

(PHP 5, PHP 7, PHP 8)

mysqli_driver::$report_mode -- mysqli_reportDefine el modo de informe de errores de mysqli

Descripción

Estilo orientado a objetos

Estilo por procedimientos

Según los flags, esto define el modo de informe de errores de mysqli a excepción, advertencia o ninguno. Cuando se define como MYSQLI_REPORT_ALL o MYSQLI_REPORT_INDEX esto también informará sobre consultas que no utilizan un índice (o un índice incorrecto).

A partir de PHP 8.1.0, el valor por omisión es MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT. Anteriormente, era MYSQLI_REPORT_OFF.

Parámetros

flags
Flags admitidos
Nombre Descripción
MYSQLI_REPORT_OFF Desactiva los informes (valor por omisión)
MYSQLI_REPORT_ERROR Informa sobre errores desde llamadas a funciones mysqli
MYSQLI_REPORT_STRICT Lanza una excepción mysqli_sql_exception para errores, en lugar de emitir alertas
MYSQLI_REPORT_INDEX Informa si no se utiliza ningún índice o un índice incorrecto en una consulta
MYSQLI_REPORT_ALL Define todas las opciones (informa sobre todo)

Valores devueltos

Siempre devuelve true.

Historial de cambios

Versión Descripción
8.1.0 El valor por omisión es ahora MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT. Anteriormente, era MYSQLI_REPORT_OFF.

Ejemplos

Ejemplo #1 Estilo orientado a objetos

<?php

/* Activación del informe de errores */
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ALL;

try {
/* si la conexión falla, se lanzará una mysqli_sql_exception */
$mysqli = new mysqli("localhost", "my_user", "my_password", "my_db");

/* esta consulta debería informar sobre un error */
$result = $mysqli->query("SELECT Name FROM Nonexistingtable WHERE population > 50000");

/* esta consulta debería informar sobre un índice incorrecto, si la columna population no tiene un índice */
$result = $mysqli->query("SELECT Name FROM City WHERE population > 50000");
} catch (
mysqli_sql_exception $e) {
error_log($e->__toString());
}

Ejemplo #2 Estilo por procedimientos

<?php

/* Activación del informe de errores */
mysqli_report(MYSQLI_REPORT_ALL);

try {
/* si la conexión falla, se lanzará una excepción mysqli_sql_exception */
$link = mysqli_connect("localhost", "my_user", "my_password", "my_db");

/* esta consulta debería informar sobre un error */
$result = mysqli_query($link, "SELECT Name FROM Nonexistingtable WHERE population > 50000");

/* esta consulta debería informar sobre un índice incorrecto, si la columna population no tiene un índice */
$result = mysqli_query($link, "SELECT Name FROM City WHERE population > 50000");
} catch (
mysqli_sql_exception $e) {
error_log($e->__toString());
}

Ejemplo #3 Informe de errores a excepción de errores de índice incorrecto

<?php

/* Activación del informe de errores */
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ALL;

try {
/* si la conexión falla, se lanzará una mysqli_sql_exception */
$mysqli = new mysqli("localhost", "my_user", "my_password", "my_db");

/* esta consulta debería informar sobre un error */
$result = $mysqli->query("SELECT Name FROM Nonexistingtable WHERE population > 50000");

/* esto NO emitirá un error incluso si no hay ningún índice disponible */
$result = $mysqli->query("SELECT Name FROM City WHERE population > 50000");
} catch (
mysqli_sql_exception $e) {
error_log($e->__toString());
}

Ver también

add a note

User Contributed Notes 2 notes

up
6
nineoclick (atsymbol) gmail (dot) com
8 years ago
Seems not clear but flags *could be combined*, as per other flags.
For example:

<?php

# wannabe noticed about all errors except those about indexes
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX;

?>
up
0
theking2 at king dot ma
3 months ago
Example #1 will now report a depricated warning:

"The mysqli_driver class is an instance of the monostate pattern, i.e. there is only one driver which can be accessed though an arbitrary amount of mysqli_driver instances."

To set the error mode of the mysql driver do use

<?php
mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
?>
To Top