-
11-10-2019 - |
题
我创建了一个名为Database.php的类,用于使用Pear DB类与MySQL数据库进行交互。
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调用此类
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时,我会收到以下错误
警告:require_once(/usr/share/pear/db.php):未能打开流:/var/www/avatar/database.php in 2 line 2致命错误:require_once() '/usr/share/pear/db.php'(include_path ='。:/usr/share/php:/usr/share/pear')
我不知道为什么我会遇到这个错误。在phpinfo()中显示 include_path .:/usr/share/php:/usr/share/pear .:/usr/share/php:/usr/share/pear
我在用 php5
我什至尝试安装 php-pear
软件包,我仍然会遇到相同的错误。我不明白这里怎么了。有人可以向我指出正确的方向吗?
笔记 : 我尚未使用使用PHP5 sudo apt-get install php5
. 。我已经下载了PHP5软件包 Keryx 应用。
解决方案
不隶属于 StackOverflow