gpt4 book ai didi

php - Zend 2 无法连接到数据库

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:22 25 4
gpt4 key购买 nike

我正在使用带有 MySQL 的 wamp 服务器来编写我的第一个 zend 2 应用程序。

我有以下问题:

Message:
Connect Error: SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'kljpeer'
Stack trace:
#0 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Db\Adapter\Driver\Pdo\Pdo.php(249): Zend\Db\Adapter\Driver\Pdo\Connection->connect()
#1 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Db\Sql\Sql.php(120): Zend\Db\Adapter\Driver\Pdo\Pdo->createStatement()
#2 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Db\TableGateway\AbstractTableGateway.php(232): Zend\Db\Sql\Sql->prepareStatementForSqlObject(Object(Zend\Db\Sql\Select))
#3 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Db\TableGateway\AbstractTableGateway.php(208): Zend\Db\TableGateway\AbstractTableGateway->executeSelect(Object(Zend\Db\Sql\Select))
#4 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Db\TableGateway\AbstractTableGateway.php(195): Zend\Db\TableGateway\AbstractTableGateway->selectWith(Object(Zend\Db\Sql\Select))
#5 D:\Git\klj-website\module\Website\src\Website\Model\PostTable.php(24): Zend\Db\TableGateway\AbstractTableGateway->select()
#6 D:\Git\klj-website\module\Website\src\Website\Controller\HomeController.php(23): Website\Model\PostTable->fetchAll()
#7 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(83): Website\Controller\HomeController->indexAction()
#8 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#9 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#10 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#13 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#14 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#15 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#16 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(313): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#17 D:\Git\klj-website\public\index.php(17): Zend\Mvc\Application->run()
#18 {main}

这是我的配置文件:global.php

return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=kljpeer;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);

这是我的配置文件:local.php.dist

return array(
'db' => array(
'username' => 'root',
'password' => '',
),
);

最佳答案

尝试将 local.php.dist 重命名为 local.php

├───config
│ │ application.config.php
│ │
│ └───autoload
│ global.php
│ local.php - your local settings

Zend Framework 2’s ModuleManager merges all the configuration from each module’s module.config.php file and then merges in the files in config/autoload (*.global.php and then *.local.php files)

User Guide - Database and models

例如:

// global.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=kljpeer;host=localhost',
...
),
);

// local.php
return array(
'db' => array(
'username' => 'YOUR USERNAME HERE',
'password' => 'YOUR PASSWORD HERE',
),
);

关于php - Zend 2 无法连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24068784/

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