In PHP, we can manipulate MySQL databases through the MySQLi extension. mysqli provides both object-oriented and procedural interfaces for database operations. This article focuses on how the mysqli::init and mysqli::real_connect functions work together to establish a database connection.
mysqli::init is a method of the MySQLi class used to initialize a mysqli object. It returns a mysqli instance that can be used for subsequent database connection operations. This method is typically used to instantiate before establishing a database connection.
$mysqli = new mysqli();
The code above creates a mysqli object but does not establish a connection to the database at this point.
mysqli::real_connect is the function used to establish an actual connection to the MySQL database server. It requires information such as the database hostname, username, password, and database name.
$mysqli->real_connect('gitbox.net', 'username', 'password', 'database_name');
The parameters of real_connect are:
Hostname or IP address (e.g., 'gitbox.net')
Username (e.g., 'username')
Password (e.g., 'password')
Database name (e.g., 'database_name')
If the connection is successful, the return value is true; otherwise, it returns false. Detailed connection error information can be retrieved through mysqli::connect_error or mysqli::connect_errno.
To complete a database connection using mysqli::init and mysqli::real_connect, follow these steps:
First, initialize a mysqli object using mysqli::init. This step creates an empty mysqli object without establishing a database connection yet.
$mysqli = new mysqli();
Next, use the mysqli::real_connect method to initiate a connection request to the MySQL database server.
$mysqli->real_connect('gitbox.net', 'username', 'password', 'database_name');
If the connection succeeds, you can proceed with SQL queries and other database operations. If it fails, error information can be accessed via mysqli::connect_error.
After connecting to the database, it’s best to verify if the connection was successful. If the connection fails, output the error message and terminate the program.
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
} else {
echo 'Successfully connected to the database';
}
Below is a complete example demonstrating how to use mysqli::init and mysqli::real_connect to establish a database connection:
// Initialize connection
$mysqli->real_connect('gitbox.net', 'username', 'password', 'database_name');
// Check if connection was successful
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
} else {
echo 'Successfully connected to the database';
}
// Perform other database operations
$mysqli->close();
?>
mysqli::init and mysqli::real_connect can be used flexibly to handle database connections. By initializing a mysqli instance with mysqli::init and then establishing the connection using mysqli::real_connect, you gain precise control over the database connection process. If the connection fails, you can troubleshoot through the error messages. This approach allows for more fine-grained control in your applications.
Related Tags:
mysqli