Sinisterly
PDO Tutorial - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: PHP (https://sinister.ly/Forum-PHP)
+--- Thread: PDO Tutorial (/Thread-PDO-Tutorial)



PDO Tutorial - Jacob - 09-26-2013

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]

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]


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]


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]

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]


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]


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!


RE: PDO Tutorial - The Alchemist - 09-26-2013

Wow, finally Jacob is back!!! That too with a bang!!!
And with PHP!!!! Cool!!!
Your tut is very cool..
Just one thing I'd suggest to change :
Always put the PDO object instantiation in a try - catch block :
PHP Code:
<?php try { $db = new PDO($dsn,DB_USER,DB_PASSWORD); }catch(PDOException $e) { die($e->getMessage()); } ?>
Or it'll give a fatal error if anything goes wrong.


RE: PDO Tutorial - Jacob - 09-26-2013

(09-26-2013, 12:46 PM)The Alchemist Wrote: Wow, finally Jacob is back!!! That too with a bang!!!
And with PHP!!!! Cool!!!
Your tut is very cool..
Just one thing I'd suggest to change :
Always put the PDO object instantiation in a try - catch block :
PHP Code:
<?php try { $db = new PDO($dsn,DB_USER,DB_PASSWORD); }catch(PDOException $e) { die($e->getMessage()); } ?>
Or it'll give a fatal error if anything goes wrong.

Everyone comes back sometime Wink

Anyhow, I dont catch any exception because if it doesn't connect properly, the script shouldn't run.


RE: PDO Tutorial - Psycho_Coder - 09-26-2013

Very good thread. I like theme of the editor as well. Good work.

EDIT : Just a Suggestion that change the thread title from PDO to PHP Data Objects, then its would be easier to understand for those who have basic knowledge of php


RE: PDO Tutorial - 1llusion - 09-26-2013

Good job Smile


RE: PDO Tutorial - bluedog.tar.gz - 09-26-2013

Well explained tutorial man!
Good use of headers, colors and images.

But..

[Image: 3BrX60.jpg]

Wat?

lol


RE: PDO Tutorial - Jacob - 09-27-2013

(09-26-2013, 04:36 PM)bluedog.tar.gz Wrote: Well explained tutorial man!
Good use of headers, colors and images.

But..

[Image: 3BrX60.jpg]

Wat?

lol

gotta fuck bitches get money.


Smile