PHP Access Database with PDO

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions.

PDO provides a data-access abstraction layer, which means that, regardless of which database you’re using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn’t rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.

pdo

Connecting to MySQL

Old Method:

New Method : all you gotta do is create a new PDO object. PDO’s constructor takes at most 4 parameters, DSN, username, password, and an array of driver options.

A DSN is basically a string of options that tell PDO which driver to use, and the connection details.

You can also set some attributes after PDO construction with the setAttribute method:

Select Statement

old Method:

New Method:

Parameters

old Method:

New Method:

Named Placeholders Method:

Transactions:

Here’s an example of using transactions in PDO: (note that calling beginTransaction() turns off auto commit automatically):


 

Refrences:-

Leave a Reply

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