我有这样的文件夹结构如何使用Doctrine DBAL?
[-] myapp/
[+] app/
[-] lib/
[-] vendor/
[-] doctrine/
[-] Doctrine/
Common/
DBAL/
Symfony/
[+] bin/
[-] sites/
[-] default/
[-] test/
test-doctrine-dbal.php
我尝试代码的文档
<?php
use Doctrine\Common\ClassLoader;
require dirname(__FILE__).'/../../../lib/vendor/doctrine/Doctrine/Common/ClassLoader.php';
$classLoader = new ClassLoader('Doctrine', dirname(__FILE__).'/../../../lib/vendor/doctrine/Doctrine/DBAL/DriverManager.php');
$classLoader->register();
$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
'dbname' => 'cdcol',
'user' => 'root',
'password' => '',
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$conn = DriverManager::getConnection($connectionParams, $config);
$sql = "SELECT * FROM cds";
$stmt = $conn->query($sql);
while ($row = $stmt->fetch()) {
echo $row['titel'];
}
?>
我也得到警告:
Warning: require(D:\xampp\htdocs\myphp\sites\default\test/../../../lib/vendor/doctrine/Doctrine/DBAL/DriverManager.php\Doctrine\DBAL\Configuration.php) [function.require]: failed to open stream: No such file or directory in D:\xampp\htdocs\myphp\lib\vendor\doctrine\Doctrine\Common\ClassLoader.php on line 148
而一个错误:
Fatal error: require() [function.require]: Failed opening required 'D:\xampp\htdocs\myphp\sites\default\test/../../../lib/vendor/doctrine/Doctrine/DBAL/DriverManager.php\Doctrine\DBAL\Configuration.php' (include_path='.;D:\xampp\php\PEAR') in D:\xampp\htdocs\myphp\lib\vendor\doctrine\Doctrine\Common\ClassLoader.php on line 148
我不太了解PHP命名空间。在PHP手册中阅读PHP命名空间仍然无法解决问题。如果我想在该目录结构中使用Doctrine DBAL,该代码是否正确?
我尝试添加此:通过set_include_path(真实路径(目录名(_ _文件_ _)'/ ../../../lib中/供应商/教义/学说/'));但仍然返回相同的错误。我在_和_中放置空格,因为double _未显示在SO – Permana
@Permana检查'realpath'是否返回字符串或布尔值。相对路径中可能有太多/很少的点或某些文件权限问题。 – takeshin
尝试“手动”提供正确的路径。使用'is_readable'检查文件。也许这是一个文件许可问题。 – takeshin