MySQLi Object-Oriented PHP Database Connection

PHP - Mysql logoBefore we do anything with our project, we need to access the data stored in the MySQL database. For that, we need to be able to connect to the database server.

  • MySQL is the most popular database system used with PHP.
  • With PHP, you can connect to and manipulate databases(insert, edit, delete data).

You can create a connect.php file that contains this functionality so that you can include(“connect.php”); anywhere you need mysql along your project.

You can use a simple – MySQLi Procedural – type of connection or you can use a MySQLi Object-Oriented connection type or a PDO connection (PHP Data Objects).

Most of the people I know are using a MySQLi Object-Oriented connection like this:

Also, the simple procedural method looks almost the same as the OOP method, with a small difference:

Most of the projects can handle the database with the two methods from above, along this, there is another method for a connection this time using PDO (PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. This means developers can write portable code much easier. PDO is not an abstraction layer like PearDB. PDO is a more like a data access layer which uses a unified API)