PHP is an open-source general-purpose scripting language, which is widely used for creating dynamic and interactive web pages. PHP can access a large range of relational database management systems such as MYSQL, SQLite, and PostgreSQL. The PHP 5.1 version offered a new database connection abstraction library, which is PHP Data Objects (PDO).
PDO refers to PHP Data Object, which is a PHP extension that defines a lightweight and consistent interface for accessing a database in PHP. It is a set of PHP extensions which provide a core PDO class and database-specific driver. Each database driver can expose database-specific features as a regular extension function that implements the PDO interface.
PDO mainly focuses on data access abstraction rather than database abstraction. It provides data-access abstraction layer, which means, regardless of which database we are using, we have to use the same functions provided by that database to issue queries and fetch data. PDO does not provide data abstraction, as it does not rewrite the SQL or emulate missing features.
PDO provides various ways to work with objects and retrieves prepared statements that make work much easier. It is a database access tool in PHP through which we enable uniform access across several databases.
PDO allows comparatively seamless switching between different databases and platforms, which can be easily done by changing the connection string. It does not support database-specific syntaxes.
There are some advantages of PDO as follows:
PDO is the native database driver. There are some benefits of using PDO that are given below:
There are three PDO classes, which are given below:
To access the database by using PHP, we have mainly two options - MySQLi and PDO (PHP Data Object). MySQLi is a native for PHP that provides faster performance, whereas, most of the experienced developers prefer to work with PDO as it supports a wide variety of database drivers. There are some differences between PDO and MySQLi listed below based on their features.
Features | PDO | MySQLi |
---|---|---|
DB Support | 12 different drivers | MySQL only |
Connection | Easy | Easy |
API | OOP | OOP + Procedure |
Performance | Fast | Fast |
Named Parameter | Yes | No |
Object mapping | Yes | Yes |
Stored Procedure | Yes | Yes |
Client-Side Prepared Statements | Yes | No |
Security | More secure than MySQLi. | Secure but not more than PDO. |
Both PDO and MySQLi have their own advantages:
There is no need of external libraries to build this extension.
Step 1: Download the latest XAMPP server from here https://www.apachefriends.org/download.html for different platforms like Windows, Linux, and MacOS.
Step 2: Install the XAMPP Server on your system by following these steps.
Step 3: Select the components which you want to install and click on the Next button.
Step 4: Create the new folder with the name xampp at the location where you want to install XAMPP.
Step 5: Click on Next here and move forward. Installation of the XAMPP server will start from here.
Step 6: XAMPP is installed successfully. Click on Finish button.
Step 7: Select the preferred Language.
Step 8: Run the Apache server and MySQL from here (as per the given screenshot).
Step 9: Now, open php.ini from C:/xampp/php/php.ini (where you have installed your XAMPP) and uncomment the extension "php_pdo_mysql.dll" and "php_pdo.dll" (if you are working with MySQL database), or "php_pdo_oci.dll" (if you are working with Oracle database). Now, start working with the database. In PHP 5.1 upper version, it is already set.
Firstly we need to create a database, so create a database by name myDB from here.
A database connection is always required to interact with the database. So, we need to know the identifier to access database, i.e., location of the database, database name, username, and password.
Now, create the database connection program using PDO in any text editor such as notepad or notepad++ and save it by coonection.php name. Run it on XAMPP server using localhost/80.
<?php $dbHost="localhost"; $dbName="myDB"; $dbUser="root"; //by default root is user name. $dbPassword=""; //password is blank by default try{ $dbConn= new PDO("mysql:host=$dbHost;dbname=$dbName",$dbUser,$dbPassword); Echo "Successfully connected with myDB database"; } catch(Exception $e){ Echo "Connection failed" . $e->getMessage(); } ?>
Output
Run it on the server using following URL localhost/Xampp/pdoexample/connection.php/ or where you have saved your program.
A PDOException object will be thrown if there is any connection error. We may catch the exception if we want to handle the error condition, or we can also leave it to global exception handler which can be set up by set_exception_handler() function.
In this example, dbUser(user-id) is wrong, so it will throw an exception, as we can see in the output.
<?php $dbHost="localhost"; $dbName="myDB"; $dbUser="rroot"; $dbPassword=""; try{ $dbConn= new PDO("mysql:host=$dbHost;dbname=$dbName",$dbUser,$dbPassword); //Set the PDO error mode to exception. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Echo "Successfully connected with myDB database"; } catch(Exception $e){ Echo "Connection failed" . $e->getMessage(); } ?>
<?php $dbHost="localhost"; $dbName="myDB"; $dbUser="root"; //by default root is user name. $dbPassword=""; //password is blank by default try{ $dbConn= new PDO("mysql:host=$dbHost;dbname=$dbName",$dbUser,$dbPassword); Echo "Successfully connected with myDB database"; } catch(Exception $e){ Echo "Connection failed" . $e->getMessage(); } // this command close the connection. $dbConn = null; ?>
Output