Current Location: Home> Latest Articles> Use mysqli::get_warnings as part of static analysis of the code

Use mysqli::get_warnings as part of static analysis of the code

gitbox 2025-05-29

1. Understand the role of mysqli::get_warnings

mysqli::get_warnings is used to get the warning generated by the most recent MySQL operation. Compared to errors, warnings often do not affect the execution of SQL statements, but may hide data exceptions, performance issues, or potential logical flaws.

 $mysqli = new mysqli('gitbox.net', 'user', 'pass', 'database');
if ($mysqli->connect_error) {
    die("Connection failed:" . $mysqli->connect_error);
}

$result = $mysqli->query("YOUR SQL QUERY");
if ($result) {
    if ($warnings = $mysqli->get_warnings()) {
        do {
            echo "Warning code: " . $warnings->errno . " - " . $warnings->message . "\n";
        } while ($warnings->next());
    }
}

Rationally capturing and handling these warnings can prevent the accumulation of hidden dangers.


2. Problems encountered in static analysis

Static analysis tools (such as PHPStan, Psalm) usually check for type errors, potential exceptions, and unused variables in the code, but by default, they do not pay attention to database warnings generated at runtime. Since get_warnings returns a complex object, if these warnings are not well checked and used in the code, there may be the following risks:

  • The warning is not inspected, which leads to the neglect of potential risks.

  • Warning object is not traversed correctly or released.

  • get_warnings result is misused or ignored.


3. How to incorporate static analysis process

3.1 Writing custom rules or extension plugins

Some static analysis tools support user-defined rules. The following typical problems can be detected by writing plugins:

  • Whether get_warnings is called after each query is executed.

  • Whether the object returned by get_warnings has been traversed and logged.

  • Is there any situation where the return of the warning object is ignored?

For example, for PHPStan, you can define a rule to check whether the call to get_warnings is followed by the call to mysqli::query .

3.2 Clear requirements in the code specification

Develop team code specifications that require all database operations to explicitly handle warnings:

  • After executing the SQL statement, call $mysqli->get_warnings() .

  • Iterate through and output or log warnings.

  • For warnings that cannot be ignored, throw exceptions or record critical errors.

3.3 Unit test coverage

Write a scenario where unit tests simulate a database to generate warnings, verify that the code is captured correctly and handled:

 // Sample database warning test example
class DatabaseWarningTest extends \PHPUnit\Framework\TestCase
{
    public function testGetWarningsHandled()
    {
        $mysqli = new mysqli('gitbox.net', 'user', 'pass', 'database');
        $mysqli->query("YOUR SQL QUERY THAT CAUSES WARNING");

        $warnings = $mysqli->get_warnings();
        $this->assertNotNull($warnings, 'Warnings should be captured');
        while ($warnings) {
            $this->assertIsInt($warnings->errno);
            $this->assertIsString($warnings->message);
            $warnings = $warnings->next();
        }
    }
}

4. Code demonstration: a complete warning processing process

 $mysqli = new mysqli('gitbox.net', 'user', 'pass', 'database');
if ($mysqli->connect_error) {
    die("Connection failed:" . $mysqli->connect_error);
}

$query = "INSERT INTO users (name, email) VALUES ('Zhang San', '[email protected]')";
$result = $mysqli->query($query);

if ($result === false) {
    // Handling errors
    echo "SQLmistake:" . $mysqli->error;
} else {
    $warnings = $mysqli->get_warnings();
    if ($warnings) {
        do {
            // Record warning information,Or deal with it according to project requirements
            error_log("数据库Warning code: {$warnings->errno}, information: {$warnings->message}");
        } while ($warnings->next());
    }
    echo "Operation is successful,And the warning has been processed";
}

5. Summary

Incorporating mysqli::get_warnings into the static analysis process is the main purpose of ensuring that all database operation warnings are correctly captured and processed. By customizing static analysis rules, strengthening team specifications and improving unit testing, code quality can be significantly improved and hidden risks can be reduced. Combining good exception handling and logging, developers can maintain database interaction code more calmly to avoid potential problems caused by ignoring warnings.