PDO Tutorial 09-26-2013, 04:37 AM
#1
This tutorial assumes you have decent PHP and SQL knowledge.
What is 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. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.
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 ships with PHP 5.1, and is available as a PECL extension for PHP 5.0; PDO requires the new OO features in the core of PHP 5, and so will not run with earlier versions of PHP."
Resource
PHP: Introduction - Manual. (n.d.). Retrieved from http://www.php.net/manual/en/intro.pdo.php
settings.php
In order to start using PDO we must define some constants which will contain connection data for our database.
Open your IDE (I use sublime text) and create a new .PHP script titled settings.php. Inside of this script we will just define the needed constants using PHP's define() function.
![[Image: 4Aidy.png]](http://puu.sh/4Aidy.png)
Now that you have the correct information for connection to your database you may save and close this script, as you will no longer need to edit it.
db.php
Create another script and entitle is db.php. This is where we are going create the PDO object which we will use across our script to modify the database.
The first step is to include settings.php so that we may access the constants we defined in the last section.
Use the require_once() function to do this. If the required file is not found then the current script will die, as opposed to the include_once() which will try to find the file and continue even if it's not found.
![[Image: 4AlNz.png]](http://puu.sh/4AlNz.png)
Once you have required settings.php you now must create your DSN (Data Source Name) variable which will hold information for PDO to connect to your database.
Follow the code in the image to format your DSN variable properly.
![[Image: 4AlZq.png]](http://puu.sh/4AlZq.png)
Now that we have our DSN variable set, we can go ahead and create the PDO instance which we will be using! The PDO constructor can take up to 4 parameters, we will only be using the first 3.
Now create a variable and follow the code below.
![[Image: 4Amju.png]](http://puu.sh/4Amju.png)
In that code we set the pdo attribute of prepare emulation to false so that we must prepare our statements manually. This is so we are dealing with prepared statements, helping to defend against SQLi attacks.
We are now through with our db.php script so you may save and close it.
script.php
This is our main script which we will be doing all database communication. All previous work will be importing into this script and will be made of use as well.
First create the new script file entitled script.php and import db.php to be able to used the PDO instance we created.
![[Image: 4AmTH.png]](http://puu.sh/4AmTH.png)
There are only 2 functions which you will need to use in order to modify the database. The PDOStatement PDO::prepare() function and the bool PDOStatement::execute() function.
PDO::prepare() prepares a statement for execution and returns a statement object -> PDOStatement
PDOStatement::execute() executes a prepared statement and returns a boolean based on the success of the statement.
The prepare() function takes an SQL query in the arguments, in any place where a variable will be inputted place a '?'.
We will assume that you have a table in your database called 'hcdata' with two columns. Both columns are VARCHAR(20) and titled 'username' and 'usergroup'.
In the following code we will take some data inputted by $_GET and put it into the database. The data will be username and usergroup, if the database doesn't contain that username then the script will add the username and usergroup. Afterwards all of the current users stored in the database will be displayed on the screen in ascending order based on their usergroup.
![[Image: 4AqdP.png]](http://puu.sh/4AqdP.png)
Live Example
I have put the scripts on my server and allowed use through an HTML form. Here you can directly see the result of the PDO we covered in this short tutorial.
Click me for a live demo!
Credits
>@1llusion for teaching me quite a bit of php, introducing me to PDO, and giving me the basic structure which is being used in this tutorial!
>@1llusion again for dealing with my constant questions. <3
>http://php.net/ The home of php, with complete documentation of everything native!
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)

![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)
![[Image: 3BrX60.jpg]](http://yolo.downloadswag.com/3BrX60.jpg)