Sunday, August 7, 2011

Installing PDO on Ubuntu

Requirements for this post:

Installed php 5.1+ engine, apache2 web server
installed Netbeans 7 or 6.9
Installed MySQL and MySQL WB.

If you havent already installed this visit my older posts and install whats missing.

So in this post I will explain what is PDO and how can be installed on Ubuntu/Windows.

PDO stands for PHP Data Objects. It is PECL extension for PHP 5.0 and ships with PHP 5.1 and newer versions of PHP. It is dependent on OO features of PHP so it will not work in older PHP versions.

It is data-access abstraction layer  which means that you can use same function for access and manipulation of db data no mather what type of db is behind. So depending on the type of the db behind, you use special PDO driver for that database.  Here is the official PDO docs PDO .

PDO is not dependent of other libraries and extensions so if you have PHP 5.0+ you are ready to go and install.

PDO is a PECL extension so you will need this package in order to install pdo php5-dev.

Open terminal and run this command:

$sudo apt-get install php5-dev

 No we will install PDO.

The easiest way to install on Ubuntu is to use these commands:

$ sudo apt-get install libmysqlclient15-dev
$ sudo pecl install pdo
$ sudo pecl install pdo_mysql
 
 I have PHP 5.3 so no need to edit the php.ini file. If you have other version 
you should edit the php.ini file and add these lines 
 
extension=pdo.so
extension=pdo_mysql.so
 
On PHP 5.3 php.ini is place in the /etc/php5/apache2 folder. 
On other PHP versions it should be in /etc/php5/cgi/php.ini or /etc/php/cli/php.ini.
 
So no open terminal and run this command 
 
$ sudo gedit /path/to/php.ini 
 
mine was 
 
$sudo gedit /etc/php5/apache2/php.ini
 
It will open text file. Find the Dynamic extensions section and add the lines
 
extension=pdo.so
extension=pdo_mysql.so 
 
Now save the file and close it. In terminal run 
 
$ sudo /etc/init.d/apache2 restart 
 
to restart the web server.   Now I will create a test project in Netbeans to test 
the installtion of PDO. 
In previous post I created new PHP project called HelloWorldPHP. If you haven't 
I will explain it again here. 
Open Netbeans and click file->new project. Choose new PHP application 
and click next. Add the name of the project HelloWorldPHP and place it in the 
sources folder /var/www/HelloWorldPHP. Click finish. This will open new 
project in the projects panel. 
 
Now, click right click on the  HelloWorldPHP node and click 
set as main project. Open the sources folder and double click the index.php file
It will open in the editor window. Now you will see a php segment 
 
<?php ?>
 
Add this line of code phpinfo(); 
 
Your page will look like 
 
<?php phpinfo(); ?>
 
 
Now open a browser and type this url: http://localhost/HelloWorldPHP/ 
or click run->run main project in Netbeans. You  should get the php info 
page with all settings. Now far below on the page you have to find PDO 
section and pdo_mysql section. If this is the case than you installed it succesfully. 
Otherwise check what's wrong and post a question. 
 
In the next post i will add some on installing PDO on Windows.

1 comment: