Rodolfo Bandeira

Rodolfo Bandeira

software, electronics, security, devops, thoughts

How to use DB::PEAR in PHP and PostgreSQL

Hello Everybody!

DB::PEAR is a class of PHP Pear that can connect your script to a lot of kinds of databases just changing one line. The connection line. Here we can see the simple example for connect through PostgreSQL and select some informations. I’m using Ubuntu linux in that example. So.. let’s go!

If you are using ubuntu, type the follows commands to install php pear, and postgresql libs in PHP.

sudo apt-get install php5-pgsql
sudo apt-get install php-db
require_once 'DB.php';

$dsn = "pgsql://YOUR_LOGIN:[email protected]_OF_DATABASE/YOUR_DATABASE";
$options = array(
  'debug' => 2,
  'portability' => DB_PORTABILITY_ALL,

$db = DB::connect($dsn, $options);

if (PEAR::isError($db)) {

$sql = "select * from YOUR_TABLE";
$result = $db->query($sql);
echo "Results: " .$result->numRows();

while ($result->fetchInto ($entry)) {
  echo "Field 1: " .$entry[0]. " ;";
  echo "Field 2: " .$entry[1]. " ;";
  echo "Field 3: " .$entry[2]. " ;";
  echo "Field 4: " .$entry[3]. " ;";
  echo "Field 5: " .$entry[4]. " ;";



Hope you enjoy!


Last modified: