PHP MySQLi
The mysqli interface is an improvement (it means "MySQL Improvement extension") of the mysql interface, which was deprecated in version 5.5 and is removed in version 7.0. The mysqli extension, or as it is sometimes known, the MySQL improved extension, was developed to take advantage of new features found in MySQL systems versions 4.1.3 and newer. The mysqli extension is included with PHP versions 5 and later.
Close connection
When we are finished querying the database, it is recommended to close the connection to free up resources.
Object oriented style
$conn->close();
Procedural style
mysqli_close($conn);
Note: The connection to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling the close connection function.
Use Case: If our script has a fair amount of processing to perform after fetching the result and has retrieved the full result set, we definitely should close the connection. If we were not to, there's a chance the MySQL server will reach its connection limit when the web server is under heavy use.
MySQLi connect
Object oriented style
Connect to Server
$conn = new mysqli("localhost","my_user","my_password");
Set the default database: $conn->select_db("my_db");
Connect to Database
$conn = new mysqli("localhost","my_user","my_password","my_db");
Procedural style
Connect to Server
$conn = mysqli_connect("localhost","my_user","my_password");
Set the default database: mysqli_select_db($conn, "my_db");
Connect to Database
$conn = mysqli_connect("localhost","my_user","my_password","my_db");
Verify Database Connection
Object oriented style
if ($conn->connect_errno > 0) {
trigger_error($db->connect_error);
} // else: successfully connected
Procedural style
if (!$conn) {
trigger_error(mysqli_connect_error());
} // else: successfully connected