PDO(PHP Data Objects) is a PHP extension to PHP's database connections. As we all know that in PHP 5.5 MySQL is deprecated.  So we have to move new extensions (PDO) for database connection. In this bellows tutorial, I’ll show you how to connect to MySQL database using PDO class.  Please follow bellows steps for connect a database using PDO. We just create bellows code for create database connection using PDO.

Step: 1

Firstly, we need to create a database for operation. I have created a new database name "myTestDatabase".

Fine, database connection is done. Now we’ll explain about above code details.

1. $host is your host name. I have use default host, “localhost”.

2. $dbname contain your database name. I have created database name “mytestDatabase”.

3. $username contains your database root username or master user name, which one you set on server installation time.

4. $password contains your database password or master password name, which one you set on server installation time.

If an exception is thrown within the try{ } block, the script stops executing and flows directly to the first catch(){ } block.

Final Code looks like below:

Step: 2

I have created a table named 'my_test_table’.  You can use bellows code to create table.

Finally table created is done. Now we need to insert data to created table. We’ll discuss about data insert, update & delete operation on next tutorial. If you face problem to connect database & create table using PDO, feel free to comment your suggestions regarding this tutorial without any hesitation.