Current Location: Home> Latest Articles> How to capture mysqli_stmt::$error information in PHP unit test

How to capture mysqli_stmt::$error information in PHP unit test

gitbox 2025-05-28

In PHP development, especially when handling database operations, it is crucial to capture and process error information. Especially when we use MySQL database, mysqli_stmt::$error is a property used to obtain SQL query errors. It can help developers discover and solve problems in a timely manner. In a unit testing environment, we can also use it to capture and process error information, thereby improving the debugging and optimization efficiency of database operations.

This article will introduce how to capture mysqli_stmt::$error error message in PHP unit tests and provide you with some debugging tips.

1. Use mysqli_stmt::$error in unit tests

First, we need to create a database connection and execute an SQL query. When performing database operations, if an error occurs, mysqli_stmt::$error will store the error message. Here is a simple example showing how to capture and handle mysqli_stmt::$error .

 <?php
// Suppose we have established aMySQLiconnect
$mysqli = new mysqli("localhost", "user", "password", "database");

// 检查connect是否成功
if ($mysqli->connect_error) {
    die("connect失败: " . $mysqli->connect_error);
}

// PrepareSQLQuery statement
$sql = "SELECT * FROM nonexistent_table"; // Intentionally wrong query

// Preprocessing statements
$stmt = $mysqli->prepare($sql);

if ($stmt === false) {
    // If preprocessing fails,Catch errors
    echo "Preprocessing statements错误: " . $mysqli->error;
} else {
    // Execute a query
    $stmt->execute();

    // Check the execution results
    if ($stmt->errno) {
        // Capture and output error messages
        echo "Execution error: " . $stmt->error;
    } else {
        echo "Query successful";
    }
}

// 关闭connect
$stmt->close();
$mysqli->close();
?>

In this example, we deliberately used a nonexistent_table table name that does not exist, which would cause the query to fail. By checking mysqli_stmt::$error and mysqli_stmt::$errno , we can capture the error message and debug it.

2. Error capture in unit tests

In a unit testing environment, we can use the PHPUnit framework to test. To capture the mysqli_stmt::$error error message, we can use PHPUnit's assertion method. Here is a basic unit test example:

 <?php
use PHPUnit\Framework\TestCase;

class DatabaseTest extends TestCase
{
    private $mysqli;

    protected function setUp(): void
    {
        // 设置数据库connect
        $this->mysqli = new mysqli("localhost", "user", "password", "database");
    }

    public function testQueryExecutionError()
    {
        // Writing an incorrect query on purpose
        $sql = "SELECT * FROM nonexistent_table";
        $stmt = $this->mysqli->prepare($sql);

        // 确保Preprocessing statements没有错误
        $this->assertFalse($stmt === false, "Preprocessing statements失败: " . $this->mysqli->error);

        // Execute a query
        $stmt->execute();

        // Check whether the execution is successful
        $this->assertEquals(0, $stmt->errno, "Query execution failed: " . $stmt->error);
    }

    protected function tearDown(): void
    {
        // 关闭数据库connect
        $this->mysqli->close();
    }
}
?>

In this unit test, we deliberately used an incorrect query to simulate a failed database operation. The $this->assertFalse and $this->assertEquals methods are used to verify that the SQL query is executed successfully. If an error occurs, we will output the corresponding error message to help us debug.

3. Capture error messages and log logs

In addition to directly outputting error information, in production environment, we can also record error information in the log for later viewing. The following example demonstrates how to log mysqli_stmt::$error information into a log file.

 <?php
// Suppose we have established aMySQLiconnect
$mysqli = new mysqli("localhost", "user", "password", "database");

if ($mysqli->connect_error) {
    die("connect失败: " . $mysqli->connect_error);
}

$sql = "SELECT * FROM nonexistent_table";
$stmt = $mysqli->prepare($sql);

if ($stmt === false) {
    error_log("Preprocessing statements错误: " . $mysqli->error, 3, "/var/log/mysql_errors.log");
} else {
    $stmt->execute();

    if ($stmt->errno) {
        error_log("Execution error: " . $stmt->error, 3, "/var/log/mysql_errors.log");
    }
}

$stmt->close();
$mysqli->close();
?>

In this example, the error_log function is used to log error information into the /var/log/mysql_errors.log file. This way, we can ensure that error information is not lost and that post-analysis can be performed conveniently.

Summarize

By capturing and handling error messages using mysqli_stmt::$error , developers can more easily debug and optimize database operations. In PHP unit tests, we can use the PHPUnit framework to catch errors and make assertions to further improve the robustness of our code. In addition, logging is also a good practice that can help us track and resolve potential problems.

If you want to write more robust database operation code through PHP, be sure to learn these tips!