Current Location: Home> Latest Articles> Print the full error log using $mysqli->errno and $mysqli->error

Print the full error log using $mysqli->errno and $mysqli->error

M66 2025-05-28
  • : This property returns the error code for the last MySQL operation. If no error occurs, it returns 0.

  • mysqli::$error : This property returns the error description information of the last MySQL operation. If no error occurs, it returns an empty string.

These two properties are very useful for developers when debugging database-related issues and can provide enough error information to help find out where the problem lies.

2. Error debugging when connecting to the database

Database connection is the first step in interacting with a MySQL server. If the database connection fails, it is usually caused by an error in the server address, username, password, or database name. Using mysqli::$errno and mysqli::$error can help us understand specific error information.

Here is a sample code that demonstrates how to use these two properties to catch and print connection errors:

 <?php
// Set database connection parameters
$servername = "m66.net";
$username = "root";
$password = "password";
$dbname = "test_db";

// create MySQLi connect
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查connect是否成功
if ($conn->connect_error) {
    echo "connect失败: " . $conn->connect_errno . " - " . $conn->connect_error;
    exit();
}

echo "connect成功!";
$conn->close();
?>

In this code, we use connect_error to check whether there is a connection problem, and print out specific error information through connect_errno and connect_error .

3. Error debugging when executing query

If the database connection is successful but an error occurs when executing the SQL query, you can use mysqli::$errno and mysqli::$error to catch the query error. Here is an example of a query error:

 <?php
// Set database connection parameters
$servername = "m66.net";
$username = "root";
$password = "password";
$dbname = "test_db";

// create MySQLi connect
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查connect是否成功
if ($conn->connect_error) {
    echo "connect失败: " . $conn->connect_errno . " - " . $conn->connect_error;
    exit();
}

// Execute a query
$sql = "SELECT * FROM non_existent_table"; // Assume that this table does not exist
$result = $conn->query($sql);

// Check whether the query is successful
if (!$result) {
    echo "Query failed: " . $conn->errno . " - " . $conn->error;
}

$conn->close();
?>

In this example, we deliberately executed a query for a non-existent table, causing the query to fail. Use errno and error to print out specific error codes and error descriptions.

4. Error log output and formatting

In actual development, error messages may require more detailed logging for later analysis and debugging. By outputting error messages to a file or log management system, it is easier to track problems. Here is an example of logging error messages to a file:

 <?php
// Set database connection parameters
$servername = "m66.net";
$username = "root";
$password = "password";
$dbname = "test_db";

// create MySQLi connect
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查connect是否成功
if ($conn->connect_error) {
    // Write error information to log file
    error_log("connect失败: " . $conn->connect_errno . " - " . $conn->connect_error, 3, "error_log.txt");
    exit();
}

// Execute a query
$sql = "SELECT * FROM non_existent_table"; // Assume that this table does not exist
$result = $conn->query($sql);

// Check whether the query is successful
if (!$result) {
    // Write error information to log file
    error_log("Query failed: " . $conn->errno . " - " . $conn->error, 3, "error_log.txt");
}

$conn->close();
?>

In this example, we use the error_log function to write the error information into the error_log.txt file. This allows you to view this log file in the later stage, which is convenient for troubleshooting problems.

5. Summary

Using the mysqli::$errno and mysqli::$error properties can help us capture and print detailed MySQL error messages in PHP. This is very helpful for debugging database connection issues and query errors. By using these properties reasonably, we can quickly locate problems and fix them, improving development efficiency.

Hope this article helps you understand how to effectively use these two properties for error debugging in MySQL database operations.