06- MySQL Connect


MySQL Connect

Since PHP 5 or later can now work with MySQL database the MySQLi or PDO_MySQL extension should be used. The functions include:

  • Mysqli_connect()
  • PDO::_construct()

Which is better to use, MySQLi or PDO?

Well the answer depends on you, but both MySQLi and PDO have their advantages:

The MySQLi can only work with MySQL databases unlike to the PDO will work on 12 different Database systems.

Meaning if you want to switch your project to use, the PDO makes the process easy, because you are no longer required to rewrite all the codes but you have only to change the connection string and few queries unlike to MySQLi, you have to recode or rewrite the entire code including the queries.

PDO and MySQLi are Object-oriented, only MySQLi offers a procedural API.


Here are the examples of MySQL in Both MySQLi and PDO


Before connecting to MySQL and access the data, make sure that the server is open.

Example of MySQLi (Object-Oriented):


Example of Closing the MySQLi (Object-Oriented):

Example of MySQLi (Procedural):

Example of Closing the MySQLi (Procedural):

Example PDO:


An example of Closing the PDO:


Go Back to our Previous lesson entitled “MySQL Database

Facebook Comments


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.