In PHP, mysqli::prepare() is a very important function for preparing SQL statements when using MySQL databases. In many cases, we want to ensure that the preparation process of SQL statements is not wrong, especially if the database operation fails. mysqli::$errno is an attribute we can use to determine whether there is an error. It can help us determine whether the prepare() method is successfully executed.
mysqli::prepare() is a function used to preprocess SQL statements. The advantage of preprocessing is that it can prevent SQL injection and improve the efficiency of database operations. In actual use, we usually send SQL statements to the MySQL server through the prepare() function, which are parsed and optimized by the MySQL server.
For example, the following code demonstrates how to use mysqli::prepare() to prepare a SQL statement:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check connection
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
exit();
}
$stmt = $mysqli->prepare("SELECT * FROM users WHERE email = ?");
?>
When using mysqli::prepare() , if there is an error in the SQL statement, prepare() will return false , and you can get detailed error information by checking mysqli::$errno .
<?php
// Prepare SQL Statement
$stmt = $mysqli->prepare("SELECT * FROM users WHERE email = ?");
if ($stmt === false) {
// if prepare() return false,That means SQL Statement有错误
echo "MySQLi prepare failed. Error number: " . $mysqli->errno . "\n";
echo "Error message: " . $mysqli->error . "\n";
} else {
// if prepare() success,Perform further operations
echo "SQL prepared successfully!\n";
}
?>
In this example, if prepare() fails, $mysqli->errno will return an error code. You can use this error code to understand the reason for the failure. $mysqli->error provides specific error information to help developers quickly locate problems.
When mysqli::prepare() execution fails, mysqli::$errno will be set to a non-zero value. Here are some common error codes:
1049 : The database does not exist.
1054 : The field does not exist (for example, the column name used in SELECT is misspelled).
1064 : SQL syntax error.
1146 : The table does not exist.
You can use these error codes to determine specific problems, such as:
if ($stmt === false) {
switch ($mysqli->errno) {
case 1049:
echo "The database does not exist!";
break;
case 1064:
echo "SQL Syntax error!";
break;
default:
echo "Unknown error: " . $mysqli->errno;
break;
}
}
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check the connection
if ($mysqli->connect_errno) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
// Prepare SQL Statement
$stmt = $mysqli->prepare("SELECT * FROM users WHERE email = ?");
if ($stmt === false) {
// if prepare() return false,Print error message
echo "MySQLi prepare failed. Error number: " . $mysqli->errno . "\n";
echo "Error message: " . $mysqli->error . "\n";
} else {
// ifsuccess
echo "SQL prepared successfully!\n";
$stmt->close();
}
$mysqli->close();
?>
Related Tags:
mysqli