PHP is one of the most popular programming languages used for creating websites. This article is all about of working with the extension PDO for PHP.PDO parse the statement and transforms it into an internal form.
What is PDO?
PDO (PHP Data Objects) is the lightweight interface that allows to work with different databases with using their specific features. With PDO, we can easily switch between different databases and manage them. PDO provides the abstract interface for accessing databases so you can use the single function for any type of database and query. You can connect to any database for which PDO has a driver.
For example, if you do not have a MySQL database but SQLite or PostgreSQL database, your server has a PDO driver for SQLite or PostgreSQL, then you will be able to connect with your database without any worry.
Which version of PHP support PDO?
PHP support PDO extension from the version PHP 5.1 and PECL (another standard extension) of PHP from version 5.0. You don’t need to add any other library or module to run this extension. As PDO is totally based of Object oriented features, it is not compatible with the older versions of PHP which doesn’t support OOP.
How to Install PDO?
Now let us discuss the installation process to expand PHP support PDO extension.By default PDO and PDO_SQLITE drivers are enabled in PHP 5.1.0.
NOTE: These settings are only available to owners of VDS (Virtual Dedicated Server – Virtual Dedicated Server) servers, Not for the shared server. So, First of all, check the status of the server extensions PDO, Mostly it is installed but not enabled and if not, clarify with you hosting provider.
UNIX Installation:
First, I will show you UNIX Installation.To setup in UNIX system, requires basic knowledge of the Unix command line via ssh.
You need to update the php.ini file while installing PDO as a module and it loads the PDO extension. The following line will be used to load the PDO extension as a module.
1 2 3 | extension=pdo.so |
Windows Installation:
You just need to activate module from php.ini as PDO is already added:
1 2 3 | extension=php_pdo.dll |
Next, You need to enable other driver specific DLL files. you can use either dll() to load dll files or you can simply enable it from php.ini file.Here is the list of extensions, you need to enable.
1 2 3 4 5 6 7 8 9 10 11 12 | extension=php_pdo.dll extension=php_pdo_firebird.dll extension=php_pdo_informix.dll extension=php_pdo_mssql.dll extension=php_pdo_mysql.dll extension=php_pdo_oci.dll extension=php_pdo_oci8.dll extension=php_pdo_odbc.dll extension=php_pdo_pgsql.dll extension=php_pdo_sqlite.dll |
After Installation, make sure to restart Apache server to load the extensions.
How to connect with PDO?
PDO object to connect to the database built from version PHP 5.1. This method of connecting to MySQL very easy.
To establish the connection with a mysql database using PDO, Create a file.As similar to MySQL connection, PDO also requires Database Host, Database User Name, Database Password, Database Name.
SYNTAX :
To make it clearer, let’s see the syntax connecting to MySQL database:
1 2 3 | $var = new PDO ("mysql: host = $host; dbname = $dbname", $user, $password); |
Here,$var is the database Handler. Here passed the host, database name, followed by a username and password
After you create a PDO object, database connection will be established automatically. All you need to do is fill in the line host name and password to the database.
How to handle connection errors?
Using above syntax, PDO will connect to the database and return the resource id if successfully connected with database and if not, an error should be set only as an exception.PDO object will throw an exception which will be handled by PDOException class. you can handle the error using PDOException class.
Here is the example to use PDOException to handle to errors:
1 2 3 4 5 6 7 8 9 10 11 | try { $dbh = new PDO ("mysql: host = $host; dbname = $dbname", $user, $password); } catch (PDOException $e) { die(); } |
How to Close connection PDO?
Every prepared statement occupies server resources. Statements should be closed explicitly immediately after use. If not closed explicitly, the statement will be closed, you can use the following syntax.
Now, To close the connection in PDO is very easy.You just need to declare the database handler to null and it will close the connection.
1 2 3 4 5 | <?php $dbh=null; ?> |
How to create Persistent connections using PDO?
A persistent connection is not closed at the end of the script.You can make the persistent connection using the PDO::ATTR_PERSISTENT attribute in PDO.
1 2 3 4 5 | <?php $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array( PDO::ATTR_PERSISTENT => true )); ?> |
Hope this article gives you the basic idea about PDO. Have you ever used PDO in your application? Do you like to use PDO in your application? Please let me know through the comments section below.