我想创建一个使用类的数据访问。但会发生问题。我的文档根的public_html所以我/资源文件是外面的文档根目录PHP - 创建数据库访问类
我在/resources/dal/task.php
<?php
namespace resources\dal;
class task
{
public $result;
public function __construct()
{
try {
# MySQL with PDO_MYSQL
$DBH = new PDO('mysql:host=localhost;dbname=test', 'root', 'abc123');
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION, PDO::NULL_EMPTY_STRING);
}
catch(PDOException $e) {
echo "I'm sorry. Please try again later.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
}
public function __destruct()
{
$DBH = null;
}
public function select()
{
$STH = $DBH->prepare("SELECT * FROM task");
$STH->execute();
$result = $STH->fetch(PDO::FETCH_ASSOC);
$this->result = $result;
}
}
?>
这里一个数据访问层类是我的主页(里面的public_html) index.php文件
<?php
spl_autoload_extensions(".php");
spl_autoload_register(function ($class) {
require $_SERVER["DOCUMENT_ROOT"] . '/../' . $class . '.php';
});
use resources\dal as DAL;
$taskClass = new DAL\task();
$result = $taskClass->select();
var_dump($result);
?>
和我得到这个错误:
Warning: require(D:/DevelopmentWebSite/public_html/../resources\dal\PDO.php): failed to open stream: No such file or directory in D:\DevelopmentWebSite\public_html\index.php on line 14
Fatal error: require(): Failed opening required 'D:/DevelopmentWebSite/public_html/../resources\dal\PDO.php' (include_path='.;C:\php\pear') in D:\DevelopmentWebSite\public_html\index.php on line 14
对不起,我正在退出e PHP新手。任何人有什么想法是什么错?
我无法在Windows非常熟悉PHP但是你尝试过改变 “/” 与“\”? – matteomattei 2012-08-11 08:52:29
我猜你没有启用PDO扩展。如果你这样做,它不会要求你的自动加载器加载'PDO'类... – DCoder 2012-08-11 09:42:52