PDO Tutorial – 02: Connect to a database using PDO

Once you agreed on the advantages of using PDO, and verified that you can use PDO on your server, is time to connect to the database. For your sake i hope that you are already having a database set up. If you don’t, I would like you to set it up.

How to connect to a database using PDO

I must remind you that the PDO extension is object oriented (OOP), which makes all the things go easier. Simply put, you can connect to the database like this:

Connecting the fast way to the database

After is all set up, you can make the replacements in the following code snippet:

Connecting the right way to the database

Yeah, there is a right way of connecting. I don’t know if this is the one, but this is the way I prefer to connect to the database:

Recommended reading:

HashPHP – http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

PHP Database Access: Are You Doing It Correctly? – http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/

PHP PDO MySQL:Example How to Connect to MySQL Using PDO Class, PDO Error Attributes – http://php.tutorialref.com/pdo-mysql-connect-example.html

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.