如何修复 `Undefined index: db_host ` in phalcon config

How to fix `Undefined index: db_host ` in phalcon config

我已经配置好服务器。最后我得到这个问题来建立联系。

根据 http://157.245.105.194/test.php

,这是新的 Linux 服务器

让我知道在以下位置给出的代码有什么问题:https://docs.google.com/document/d/1M0iqC4_5kGQVgR6aKhKFAwxbpwTOyYN7PyEgjBfL3mQ/edit?usp=sharing

http://157.245.105.194/styleogram/fs/site/wwwroot/public/

我们得到:

===========
Notice: Undefined index: db_host in /var/www/html/styleogram/fs/site/wwwroot/app/config/config.php on line 56

Notice: Undefined index: db_user in /var/www/html/styleogram/fs/site/wwwroot/app/config/config.php on line 57

Notice: Undefined index: db_password in /var/www/html/styleogram/fs/site/wwwroot/app/config/config.php on line 58

Notice: Undefined index: db_name in /var/www/html/styleogram/fs/site/wwwroot/app/config/config.php on line 59

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)' in /var/www/html/styleogram/fs/site/wwwroot/app/config/config.php:60 
Stack trace: 
#0 [internal function]: PDO->__construct('mysql:host=;dbn...', NULL, NULL, Array) 
#1 [internal function]: Phalcon\Db\Adapter\Pdo->connect(Array) 
#2 /var/www/html/styleogram/fs/site/wwwroot/app/config/config.php(60): Phalcon\Db\Adapter\Pdo->__construct(Array) 
#3 [internal function]: {closure}() 
#4 [internal function]: Phalcon\Di\Service->resolve(NULL, Object(Phalcon\Di\FactoryDefault)) 
#5 [internal function]: Phalcon\Di->get('db', NULL) 
#6 [internal function]: Phalcon\Di->getShared('db') 
#7 [internal function]: Phalcon\Mvc\Model\Manager->_getConnection(Object(Article), NULL) 
#8 [internal function]: Phalcon\Mvc\Model\Manager->getReadConnection(Object(Article)) 
#9 [internal function]: Phalcon\Mvc\Model->getReadConnection() 
#10 [internal function]: Phalcon\Mvc\Model\Query->_executeSelect(Array, Array, NU in /var/www/html/styleogram/fs/site/wwwroot/app/config/config.php on line 60
===========

您看到的错误是因为您的 $_SERVER 超全局不包含您在代码中查找的元素。您可以在您发布的 phpinfo() 页面中轻松看到这一点(查看 Apache 信息部分)。

$connection = new MysqlAdapter(
    [
        "host"     => $_SERVER['db_host'],
        "username" => $_SERVER['db_user'],
        "password" => $_SERVER['db_password'],
        "dbname"   => $_SERVER['db_name']
    ]
];

您可以删除对 $_SERVER 数组的引用,并使用包含实际安装信息的字符串。例如 $_SERVER['db_host'] 对你来说可能是 localhost

完成这些替换后,检查错误是否仍然存在 - 如果没有,则说明您已正确连接到数据库。

您也可以使用 Phalcon\Config 对象来存储配置数据,例如上面定义的数据库连接参数,而不是将它们设置在 $_SERVER 数组中。

或者,如果您希望此信息位于 $_SERVER 数组中,则必须在您的 apache 信息的虚拟主机中使用 SetEnv

How to use the setEnv variable in apache?