Pear DB class not found

Searock picture Searock · Dec 19, 2010 · Viewed 8.8k times · Source

I have create a class called Database.php for interacting with MySql database using Pear Db class.

Database.php

<?php 
require_once('DB.php');
require_once('cException.php');

class DataBase
{

    private $dsn = 'mysql://root:xxxxxx@localhost/avatar';
    private $conn;


    //Constructor
    function __construct() 
    {
        global $conn;
        $this->conn = DB::connect($dsn);
        if(DB::isError($conn))
        {
            throw new DatabaseConnectionException();
        }
    }

    //destructor
    function __destruct() 
    {
       $this->conn->disconnect();
    }

    public function select($query)
    {
        $conn->setFetchMode(DB_FETCHMODE_ASSOC);
        $result = & $conn->query($query);

        if(DB::isError($result))
        {
            return new SelectCommandException($result->getMessage());
        }

        return $result;
    }

    static public function instance()
    {
        static $objDB;

        if(! isset($objDB))
        {
            $objDB = new DataBase();
        }

        return $objDB;
    }
?>

And I am calling this class from a sample file test.php

test.php

<?php

ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);


    require_once 'Database.php';

    try 
    {
        $db = DataBase::instance();
    }
    catch (DatabaseConnectionException $ex1)
    {
        echo $ex1->toString();
    }

    try 
    {
        $sql = "Select * from register";
        $result = $db->select($sql);
        var_dump($result);
    }
    catch (SelectCommandException $ex2)
    {
        echo $ex2->toString();
    }
?>

When I run test.php I get the following error

Warning: require_once(/usr/share/pear/DB.php): failed to open stream: No such file or directory in /var/www/Avatar/Database.php on line 2 Fatal error: require_once(): Failed opening required '/usr/share/pear/DB.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/Avatar/Database.php on line 2

I don't know why I am getting this error. In phpinfo() it shows include_path .:/usr/share/php:/usr/share/pear .:/usr/share/php:/usr/share/pear

I am using php5 and I even tried installing php-pear package, still I get the same error. I don't understand what's wrong here. Can someone please point me in a right direction.

Note : I have not installed php5 using sudo apt-get install php5. I have downloaded php5 packages using Keryx application.

Answer

ajreal picture ajreal · Dec 19, 2010

Looks you did not install DB package, try in command prompt, do

pear list

If no package of DB is installed, you can installed it with

pear install DB

example from documentation