Tuesday, January 13, 2015

PDO TUTORIAL SERIES 2

Before we continue with coding, i want to explain to you why you should switch to PDO with MYSQL instead of normal MYSQL.

Why PDO?

1. PDO is consistent no matter the database. The interface is consistent across multiple database. This basically means you use the same functions/methods no matter the database type you're working with.



2. PDO is more portable. Requires minimal coding. Meaning you are able to get things done faster. (It provides more functionality with less amount to code.)

3. It is an Object Oriented API, and that's a whole lot of advantage on its own.

4. With PDO you can use binded params (later tutorial) and that will prevent most sql injection attacks (later tutorial).

5. and finally...
Image Source: php.net
 
MYSQL is fading away.

So even if no reason above is enough to switch to PDO, then the fact that it is gradually fading away should be enough. In technical terms, it is deprecated as of PHP 5.5.0.

I believe you now understand why you must follow this tutorial and even do further reading on your own.

For today, we are going to connect to a database,

Here are the details we will use through this tutorial, you can create your database if you choose to follow:

Database Name:pdo_class
Username: root
Password: ""
Server/host: localhost

Table Name: user
Fields: id(INT[9], PRIMARY KEY, AUTO INCREASE), name (VARCHAR[255]), status(INT[1]).

The general syntax for connecting to a database is:

variable name = new PDO("Database Type: Database Connection String");

Therefore, we have:

$pdo = new PDO("mysql:host=localhost;dbname=pdo_class","root","");

This statement should establish your connection, do what i did to test your connection:


 If your connection is not established, you should debug and ensure you have followed steps in tutorial one.

PDO can use exceptions to handle errors, which means anything you do with PDO should be wrapped in a try/catch block. You can force PDO into one of three error modes by setting the error mode attribute on your newly created database handle. 

Here are the syntax:



This is the default error mode. If you leave it in this mode, you'll have to check for errors in the way you're probably used to if you used the mysql.
This mode will issue a standard PHP warning, and allow the program to continue execution. It's useful for debugging.
This is the mode you should want in most situations. It fires an exception, allowing you to handle errors gracefully and hide data that might help someone exploit your system.

Now, lets do our connection correctly:

To test out exception, will enter an incorrect host:

  
 With this, i get an error when i run the code: 





Of course, it is never a good idea to output such raw error message to the public, so i usually do this:


My error log function:


PS: Create the log folder in your server.

Now, when i run the wrong connection, i get a clean message output to my user:

I can then access my log file, in the server to see what went wrong and even trace the specific user IP...


Although, this tutorial is not meant for functions, you should however note that the error log file function logs files daily, meaning, all errors for the day will be logged in the same file, if another error occurs next day, another file will be created and all errors for that day goes into the new file... and that's why i used the naming convention of adding the specific day to the file name so i can easily trace it... this is also necessary to avoid too large error files.

You can make it weekly.


I am open to suggestions, questions and corrections, drop a comment to do that.

Don't forget to share.