In previous post I have introduced some basic introduction about the PDO ( PHP DATA OBJECT ).
In this post I will describe how to establish connection with mysql database using PDO.
To make connection with database you should have following details.
To make connection with mysql database below code is used.
Above code will connect to the database if all details are correct. This code will return the resource id if database connected successfully. Here $dbh stands for Database Handler.
Now what happen if the given details are incorrect? So for this case PDO has developed their own exception class. This class called PDOException.
Using this class you can handle the error as you want. Below is the example for that.
Now closing the connection in PDO is so easy. Just declare the database handler to null will close the connection.
In next tutorial I will explain how to fetch data using PDO and Prepare Statement in PDO.
Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.
View all posts by: Avinash