Current Location: Home> Latest Articles> Using mysqli_stmt::$error to debug problems in preprocessing statements

Using mysqli_stmt::$error to debug problems in preprocessing statements

gitbox 2025-05-28

When using PHP to operate MySQL databases, prepared statements can effectively prevent SQL injection and improve security and code maintainability. However, various errors may occur during the writing and executing preprocessing statements, such as syntax errors, parameter binding errors, or execution failures.

To quickly locate these problems, mysqli_stmt::$error is a very useful debugging tool. It is a property in the mysqli_stmt class. It can return specific error information when the execution statement fails, helping developers quickly find the problem.

Basic usage

Here is a basic example using mysqli_stmt::$error :

 <?php
$mysqli = new mysqli("localhost", "db_user", "db_pass", "test_db");

if ($mysqli->connect_errno) {
    die("Connection failed: " . $mysqli->connect_error);
}

$sql = "INSERT INTO users (username, email) VALUES (?, ?)";
$stmt = $mysqli->prepare($sql);

if (!$stmt) {
    die("Preprocessing failed: " . $mysqli->error);
}

// Written the parameter binding incorrectly:Assume that two parameters should be bound,Only one is bound here
$stmt->bind_param("s", $username);

$username = "alice";
$email = "[email protected]";

if (!$stmt->execute()) {
    // use mysqli_stmt::$error Output detailed error
    echo "Execution failed: " . $stmt->error;
}

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

The output may be similar to:

 Execution failed: Number of variables doesn't match number of parameters in prepared statement

This error prompt clearly points out the problem of mismatch in the number of parameters.

When to use mysqli_stmt::$error

You can use this property for debugging in the following situations:

  1. When preprocessing statement creation fails <br> Use $mysqli->error to get error information.

  2. When parameter binding fails or number mismatch
    bind_param() itself will not report an error, but if the number of parameters is incorrect, $stmt->error will prompt when executing.

  3. When execution fails (if unique constraints are violated)
    For example, when trying to insert a duplicate email, $stmt->error will return something like
    Tips for "Duplicate entry '[email protected]' for key 'email'" .

Use with debug logs

You can also log error information to the log for subsequent troubleshooting:

 if (!$stmt->execute()) {
    error_log("Preprocessing execution error: " . $stmt->error);
}

This can avoid exposing errors to users in a production environment, and also facilitate developers to troubleshoot problems.

Practical advice

  • Don't just rely on return false , be sure to view the details in combination with the error attribute.

  • The development stage can directly output errors; logs should be recorded in the production environment.

  • Used with errno , you can get error codes for more detailed judgments.

summary

mysqli_stmt::$error is a very practical tool in debugging preprocessing statements. Through it, you can quickly know why the statement fails, thereby speeding up development efficiency and reducing troubleshooting time.

During the development process, using this attribute rationally will make your database interaction more robust and reliable.