找不到梨DB类

问题描述:

我创建了一个名为Database.php的类,用于使用Pear Db类与MySql数据库进行交互.

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;
    }
?>

我正在从示例文件test.php调用此类

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();
    }
?>

运行test.php时出现以下错误

When I run test.php I get the following error

警告: require_once(/usr/share/pear/DB.php): 无法打开流:无此文件或 目录在 第2行上的/var/www/Avatar/Database.php 致命错误:require_once():失败 需要开放 '/usr/share/pear/DB.php' (include_path ='.:/usr/share/php:/usr/share/pear') 在/var/www/Avatar/Database.php中 第2行

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

我不知道为什么会收到此错误.在phpinfo()中,它显示include_path .:/usr/share/php:/usr/share/pear .:/usr/share/php:/usr/share/pear

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

我正在使用php5,甚至尝试安装php-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.

注意::我尚未使用sudo apt-get install php5安装php5.我已经使用 Keryx 应用程序下载了php5软件包.

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

看起来您没有安装DB软件包,请在命令提示符下尝试,做

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

pear list

如果未安装DB软件包,则可以使用

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

pear install DB

文档中的示例