Current Location: Home> Latest Articles> The difference between mysqli_stmt::$error and mysqli::errno and combined use techniques

The difference between mysqli_stmt::$error and mysqli::errno and combined use techniques

gitbox 2025-05-19

In PHP, the mysqli extension provides rich functions to operate on MySQL databases. mysqli_stmt::$error and mysqli::errno are two important properties related to error handling that can help developers catch and diagnose problems in database operations. This article will explore the differences between these two properties and how to use them in combination to improve debugging efficiency.

1. The role of mysqli_stmt::$error

mysqli_stmt::$error is a property used to obtain error messages related to the current preprocessing statement object ( mysqli_stmt ). This property returns a string containing error messages that occur when executing preprocessing statements.

For example, when you execute an SQL query, if the query fails, mysqli_stmt::$error will return the error message of the MySQL server.

Sample code:

 <?php
$mysqli = new mysqli("localhost", "user", "password", "database");

// Check database connections
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Prepare SQL Statement
$stmt = $mysqli->prepare("SELECT * FROM users WHERE id = ?");
if (!$stmt) {
    echo "PrepareStatement失败: " . $mysqli->error;
    exit;
}

// Bind parameters and execute
$id = 1;
$stmt->bind_param("i", $id);
$stmt->execute();

// Check the execution results
if ($stmt->error) {
    echo "Execution error: " . $stmt->error;  // Output detailed error information
}
?>

In the above code, $stmt->error is used to get the error message that occurs when executing SQL statements. This is very useful for debugging and troubleshooting.

2. The role of mysqli::errno

mysqli::errno is a property used to get the error code for the last MySQL operation. Unlike the $error attribute that returns detailed error information, errno returns an integer value, indicating the specific number of the MySQL error.

Sample code:

 <?php
$mysqli = new mysqli("localhost", "user", "password", "database");

// Check database connections
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// implement SQL Query
$result = $mysqli->query("SELECT * FROM non_existing_table");
if (!$result) {
    echo "MySQL Error code: " . $mysqli->errno;  // 输出Error code
    echo "MySQL error message: " . $mysqli->error;  // Output detailed error information
}
?>

In this example, if the query fails, $mysqli->errno returns a MySQL error code, for example, 1146 means "the table does not exist" error, while $mysqli->error returns a detailed error message of "Table 'database.non_existing_table' doesn't exist".

3. The difference between mysqli_stmt::$error and mysqli::errno

Although both mysqli_stmt::$error and mysqli::errno are related to MySQL errors, there are several important differences between them:

  • mysqli_stmt::$error is the error message for the preprocessing statement ( mysqli_stmt ), and mysqli::errno is the error code for the MySQL connection. mysqli_stmt::$error provides an error message string, which is usually used to output to developers. Mysqli::errno returns an error code, which is often used for further error handling and diagnosis in programs.

  • Different scopes of action : mysqli_stmt::$error is only applicable to SQL statements executed via mysqli_stmt , while mysqli::errno is applicable to the entire database connection and is applicable to all types of queries and operations.

4. Use mysqli_stmt::$error and mysqli::errno to improve debugging efficiency

When you are debugging PHP database code, using mysqli_stmt::$error or mysqli::errno alone can help you find the problem, but using the two together will help you quickly locate the root cause of the problem.

  • Debugging suggestions : You can first check mysqli::errno to get an error code, and then use mysqli_stmt::$error or mysqli::error to get detailed error information. By combining these two, you can more accurately determine which part is wrong and take appropriate measures.

Combined usage example code:

 <?php
$mysqli = new mysqli("localhost", "user", "password", "database");

// Check database connections
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// implement SQL Query
$result = $mysqli->query("SELECT * FROM non_existing_table");
if (!$result) {
    echo "MySQL Error code: " . $mysqli->errno . "\n";  // 获取Error code
    echo "MySQL error message: " . $mysqli->error . "\n";  // 获取详细error message
}
?>

In the above code, first get the error code through mysqli::errno , and then get the detailed error information through mysqli::error . Such a combination can help developers quickly diagnose problems, especially when error codes and error messages complement each other.

5. Summary

  • mysqli_stmt::$error is used to get error information related to preprocessing statements, while mysqli::errno is used to get error code for database connections.

  • They each provide different error message formats, the former returns a string and the latter returns a number.

  • By using the two, you can debug MySQL operations more efficiently, quickly discover and resolve issues.

These two attributes are very important debugging tools in PHP development. Mastering their usage can significantly improve debugging efficiency and reduce errors in database operations.