gpt4 book ai didi

php - 如何使用 Doctrine DBAL?

转载 作者:搜寻专家 更新时间:2023-10-31 20:51:45 26 4
gpt4 key购买 nike

我有这个文件夹结构

[-] 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,该代码是否正确?

最佳答案

您需要将 Doctrine 库路径添加到 include_path 中。请参阅 get_include_pathset_include_path PHP 函数。

关于php - 如何使用 Doctrine DBAL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6729592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com