The historical mysql_errno() function returned the numeric error code for the original MySQL extension. Current applications can work with SQLSTATE plus a driver-specific code.
<?php
// Historical only: removed from PHP 7+
$query = mysql_query('SELECT new_field FROM student');
echo mysql_errno();
?>
An unknown column commonly produces MySQL driver error 1054, but application logic should not assume one hard-coded code covers every failure.
<?php
$stmt = $dbo->prepare('SELECT * FROM nonexistence_table');
if (!$stmt->execute()) {
$info = $stmt->errorInfo();
echo 'SQLSTATE: ' . $info[0] . '<br>';
echo 'Driver code: ' . $info[1];
}
?>
SQLSTATE: 42S02
<?php
try {
$dbo->query('SELECT * FROM nonexistence_table');
} catch (PDOException $e) {
echo htmlspecialchars($e->getCode(), ENT_QUOTES, 'UTF-8');
}
?>
<?php
try {
$connection->query('SELECT no_name FROM student');
} catch (mysqli_sql_exception $e) {
echo $e->getCode();
}
?>
Pair codes with safe error-message handling; codes are useful for logs and targeted recovery, while visitors should normally see a generic message.
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.