PHP mysqli extension for MYSQL and MARIADB databases

Nerd, Solutions 

PHP is the server side programming language and it can be used to create a wide variety of applications that can run and be accessible via web browser. These applications can include databases for data storage. PHP can be used to access, manipulate and store data into those databases. One of the popular database, that can be used with PHP is MYSQL or MARIADB.


In order for PHP to access those databases, we need to use one of built-in drivers for particular database, that we wish to use in our project. For using PHP with MYSQL or MARIADB, we use the built-in driver MYSQLI driver.

MYSQLI extension

MYSQLI can be used either as an object, in Object Oriented Programming approach or can be used as functional programming by calling functions that start with mysqli_* followed by the task you wish to complete. Like mysqli_select_db($db_connection, database_name); this function will select the database specified in the argument of the function.


In this approach of using MYSQLI we first need to create an object by instantiate mysqli constructor. The object can be used to make calls to built-in mysqli methods. 

Code example:

The argument database name, port number, and socket are optional. We can connect to our test database that is hosted locally in our machine like this:


The above code will try to connect to our database that is hosted locally, that's why we specify host as localhost, and the local_user will be used as the username with the password local_password. When the connection to the database is successfully established, the mysqli object will be returned and stored into the $mysql_object variable. But when the connection failed to be established to the database, we can use these functions to check what is the error.

After the database connection has been established successfully, we can use the arrow notation to access the built-in methods of the object. One of the recommended step after connection to the database is to set character set for the database connection. This is achieved by calling set_charset method.

Code example:

The above code will set the character set for the database connection to utf8 encoding.

Also, we can get character set information from out database connection. This is achieved by using the method get_charset(). This method will return an object with all character set encoding regarding our database connection.


The only difference between object oriented approach and functional approach is, in object oriented approach we need to create an object instance, which we can use to invoke our methods. And in functional approach we need to open database connection and store the link to that connection in a variable which we can use to talk to the database, when we invoke mysqli_* functions.

Code example of for functional approach:

This function takes the same amount of argument as mysqli constructor take. And to set our database connection character set in functional approach we use the function mysqli_set_charset($db_connection, "utf8");

As you see in the above example, we need to provide our database connection link to mysqli_set_charset() function. This is because the function needs to know which database connection to set its character set to utf-8 character encoding. Many mysqli functions works in this way.

We can also get our database connection character set information in functional approach. This is how we can achieve it, To get our database connection character encoding information in functional approach we use the mysqli_get_charset($db_connection) function. This function will return an object with all character set information of our database connection.

 Here you can learn more about MYSQLI PHP extension methods and functions