In PHP, mysqli is a commonly used database extension, which provides multiple ways to interact with the database. The mysqli_stmt class is part of the mysqli extension, which is used to execute preprocessing statements. A common requirement is to capture and display error messages when executing SQL queries to help developers debug and fix problems. This article will introduce how to obtain detailed error information when executing SQL statements through mysqli_stmt::$error .
When using preprocessing statements, we can execute the query through the mysqli_stmt object. The mysqli_stmt::$error variable saves the error information when executing the query. If SQL execution is successful, the value is an empty string; if SQL execution fails, an error description is returned.
The following is a simple example showing how to use mysqli_stmt::$error to get error information when executing SQL queries.
<?php
// Create a database connection
$mysqli = new mysqli("localhost", "username", "password", "database_name");
// Check if the connection is successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Prepare SQL Statement
$sql = "SELECT * FROM users WHERE id = ?";
$stmt = $mysqli->prepare($sql);
// 检查Statement是否Prepare成功
if ($stmt === false) {
die("PrepareStatement失败: " . $mysqli->error);
}
// Bind parameters
$id = 1;
$stmt->bind_param("i", $id);
// Execute a query
if (!$stmt->execute()) {
echo "implement SQL Statement时出错: " . $stmt->error;
} else {
echo "Query successful!";
}
// 关闭Statement和连接
$stmt->close();
$mysqli->close();
?>
In the code, $stmt->error is used to capture error information when SQL query is executed.
If the query execution fails, $stmt->error will contain a specific error description, which can help developers locate the problem.
If the query is executed successfully, $stmt->error will return an empty string.
After obtaining the error information through $stmt->error , we can take different debugging measures based on different error information. Common errors may include SQL syntax errors, table or field name spelling errors, or parameter binding errors.
In production environments, in order to avoid exposing error information to end users, developers usually log error information into log files rather than directly displaying it on the page.
<?php
// Create a database connection
$mysqli = new mysqli("localhost", "username", "password", "database_name");
// Check if the connection is successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Prepare SQL Statement
$sql = "SELECT * FROM users WHERE id = ?";
$stmt = $mysqli->prepare($sql);
// 检查Statement是否Prepare成功
if ($stmt === false) {
// Log error log
error_log("Prepare SQL Statement失败: " . $mysqli->error, 3, "/path/to/error_log.txt");
die("PrepareStatement失败");
}
// Bind parameters
$id = 1;
$stmt->bind_param("i", $id);
// Execute a query
if (!$stmt->execute()) {
// Log error log
error_log("implement SQL Statement时出错: " . $stmt->error, 3, "/path/to/error_log.txt");
echo "Query failed,Please check the log file。";
} else {
echo "Query successful!";
}
// 关闭Statement和连接
$stmt->close();
$mysqli->close();
?>
When the query is executed, the error_log() function writes the error message to the log file. This can avoid exposing detailed error information to the end user and improve application security.
Error log files should be placed in a secure location and are accessible only to developers or administrators.
Through mysqli_stmt::$error , we can easily obtain detailed error information when executing SQL query, helping us quickly debug and solve problems. Developers can decide whether to display the error message directly or record it in a log file based on actual needs. When using mysqli extension, capturing error information is a very important operation and can effectively improve the robustness and security of the application.