The removed mysql_error() function returned the text of the most recent error from PHP's original MySQL extension. Current PDO and MySQLi applications should use exceptions for normal application flow and log technical details away from visitors.
<?php
// Historical only: removed from PHP 7+
$query = mysql_query('SELECT new_field FROM student');
echo mysql_error();
?>
Unknown column 'new_field' in 'field list'
<?php
try {
$dbo = new PDO($dsn, $user, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$stmt = $dbo->query('SELECT no_name FROM student');
} catch (PDOException $e) {
error_log($e->getMessage());
echo 'Database request failed.';
}
?>
If code is intentionally using a non-exception error mode, errorInfo() returns SQLSTATE, a driver-specific code, and the driver message.
<?php
$info = $stmt->errorInfo();
print_r($info);
?>
Array
(
[0] => 42S22
[1] => 1054
[2] => Unknown column 'no_name' in 'field list'
)
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$result = $connection->query('SELECT no_name FROM student');
} catch (mysqli_sql_exception $e) {
error_log($e->getMessage());
echo 'Database request failed.';
}
?>
Continue with SQLSTATE and database error codes or server-side error notification.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.