It has happened to me several times that I find myself generating a query, either SELECT
, INSERT
or UPDATE
in PHP
, which fails and I quickly cannot figure out why!
Example 1:
$query = 'INSERT INTO usuarios (null, "Marcos")';
$resultado = $mysqli->query($query);
// ¿Por que $resultado es false?
Example 2:
$query = 'SELECT id, name FROM usuarios';
$resultado = $mysqli->query($query);
// ¿Por que $resultado es false?
What is the way in PHP to get the reason why my query failed?
MySQL i
Using direct queries:
In order to get the error generated by the last query we can use
mysqli_error
Object-oriented style example:
Procedural style example:
Using prepared queries :
In order to obtain the error generated by the last statement invoked, we can use
mysqli_stmt_error
Object-oriented style example:
Procedural style example:
PDO
Using direct queries
In order to get the error associated with the last operation we can use
PDO::errorInfo
Using prepared statements
In order to obtain the error associated with the last operation of the statement handler we can use
PDOStatement::errorInfo