我创建了一个名为 Database.php 的类,用于使用 Pear Db 类与 MySql 数据库进行交互。
数据库.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 调用此类
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 时出现以下错误
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
我正在使用 php5
,我什至尝试安装 php-pear
包,但我仍然遇到同样的错误。
我不明白这里出了什么问题。有人可以指出我正确的方向吗。
注意:我没有使用 sudo apt-get install php5
安装 php5。我已经使用 Keryx 下载了 php5 包申请。
最佳答案
关于php - 找不到 Pear DB 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4483546/