MOCKSTACKS
EN
Questions And Answers

More Tutorials









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


Conclusion

In this page (written and validated by ) you learned about PHP MySQLi . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP SQLite3.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.