During the development process, various errors often occur in database interactions, and how to catch and debug these errors is one of the techniques that developers must master. In PHP, the MySQLi extension provides a very convenient way to execute and debug database queries. Among them, mysqli_stmt::$error is a very useful property that can help us obtain error information during the execution of preprocessing statements.
This article will introduce how to use mysqli_stmt::$error to catch and debug errors in MySQLi queries, and help you better understand how to use them through actual code examples.
mysqli_stmt::$error is an attribute of the mysqli_stmt class in MySQLi. It returns the error message generated by the most recent execution of the preprocessing statement ( mysqli_stmt ). If no error occurs in the execution, it returns an empty string.
In database operations, capturing error messages is very important for debugging, especially when complex SQL queries or connection problems. mysqli_stmt::$error provides an easy way to catch these errors.
Establish a database connection
First, we need to establish a connection to the database through the mysqli class.
<?php
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'test_db';
// Create a database connection
$conn = new mysqli($host, $username, $password, $database);
// Check if the connection is successful
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Prepare SQL statements
Next, we use the mysqli_prepare() function to prepare an SQL statement and check whether the statement is prepared successfully.
<?php
$sql = "INSERT INTO users (username, email) VALUES (?, ?)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("SQL Statement preparation failed: " . $conn->error);
}
?>
Bind parameters
In MySQLi, we can bind variables to parameters in SQL queries through the bind_param() function. Make sure that the bound parameter type is consistent with the expected type.
<?php
$username = 'john_doe';
$email = '[email protected]';
$stmt->bind_param("ss", $username, $email); // The first parameter "ss" Parameters representing two string types
?>
Execute a query and catch an error
Now we are ready to execute the preprocessing statement. If the query execution fails, we can get detailed error information through mysqli_stmt::$error .
<?php
if (!$stmt->execute()) {
echo "Failed to execute the query: " . $stmt->error;
} else {
echo "Data insertion successfully!";
}
?>
mysqli_stmt::$error is very useful when debugging, especially when SQL queries are not as expected. It can help you get the error message returned by the MySQL database. For example, if there is a syntax error, the table does not exist, or the data type does not match, mysqli_stmt::$error will provide a detailed error description.
For example, suppose we execute a query containing the error syntax:
<?php
$sql = "INSERT INTO users (username, email) VALUE (?, ?)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("SQL Statement preparation failed: " . $conn->error);
}
$stmt->bind_param("ss", $username, $email);
if (!$stmt->execute()) {
echo "Failed to execute the query: " . $stmt->error; // Detailed error information will be displayed here
}
?>
On execution failure, $stmt->error may return an error message similar to the following:
Failed to execute the query: Unknown column 'VALUE' in 'field list'
This shows that we are using the wrong syntax in the SQL statement, and the correct syntax should be VALUES instead of VALUE .
Syntax error
Error prompts: Unknown column , You have an error in your SQL syntax .
Workaround: Check whether the column names, table names, and keywords in the SQL query are spelled correctly.
Data type mismatch
Error prompts: Incorrect integer value , Data truncated for column .
Workaround: Make sure that the incoming parameter type is consistent with the column type in the database table. For example, when passing a string to a column that is expected to be an integer, an error of type mismatch will occur.
Connection failed
Error prompt: MySQL server has gone away .
Workaround: Check if the database connection is still valid, especially if it is not used for a long time.
By using mysqli_stmt::$error we can effectively catch and debug errors in MySQLi queries. It not only helps developers quickly locate problems, but also improves the robustness of code and error handling capabilities. During the development process, good error handling and debugging methods are the key to improving efficiency. I hope this article can help you better understand and use mysqli_stmt::$error , making you more handy when developing MySQLi applications.