Zend数据库连接失败

发布于 2024-12-10 08:49:25 字数 3208 浏览 0 评论 0原文

好吧,我真的很难解决这个问题。我正在尝试从 zend 应用程序连接到 mysql 数据库,但收到以下错误:

Message: No database adapter present

我已经检查并仔细检查了连接凭据,它们应该没问题。代码也应该没问题,因为它在开发环境中工作正常。如果我在开发环境中故意将密码更改为不正确,我会得到完全相同的错误,这使我相信也许就是这种情况,尽管我进行了检查!

任何想法都会非常受欢迎。如果这里没有任何明显的错误,那么也许我需要查看 server/db/php 设置?

谢谢!

引导代码:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initPlaceholders(){
    Zend_Session::start();

    $this->bootstrap('View');
    $view = $this->getResource('View');

    $view->doctype('XHTML1_STRICT');

    // Set the initial stylesheet:
    $view->headLink()->appendStylesheet('/css/global.css');

    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->registerNamespace('Pog_');


    Zend_Controller_Action_HelperBroker::addPath(
    APPLICATION_PATH . '/controllers/helpers',
        'Application_Controller_Action_Helper_');

}


}

配置文件:

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.view[] =
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view.helperPath.View_Helper = APPLICATION_PATH "/views/helpers"
database.adapter         = pdo_mysql
database.params.host     = localhost
database.params.username = user
database.params.password = password
database.params.dbname   = test

数据库连接助手:

/**
 * Constructor: initialize plugin loader
 *
 * @return void
 */
public function __construct()
{
    try{

        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
        $dbAdapter = Zend_Db::factory($config->database);
        $dbAdapter->getConnection();
        $this->connection = $dbAdapter;
    } catch (Zend_Db_Adapter_Exception $e) {
        echo 'perhaps a failed login credential, or perhaps the RDBMS is not running';
    } catch (Zend_Exception $e) {
        echo 'perhaps factory() failed to load the specified Adapter class';
    }
}
public function getDbConnection(){
    return $this->connection;
}
}

指数:

// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
        ->run();

Ok I am having real difficulty solving this. I'm trying to connect to a mysql database from a zend application and i receive the following error:

Message: No database adapter present

I have checked and double checked the connection credentials and they should be fine. The code should be fine too as it works ok in the development environment. If I deliberately change the password to be incorrect in the development environment, I get exactly the same error, which leads me to believe that maybe this is the case, despite my checking!

Any thoughts would be very welcome. If there's nothing obviously wrong here then maybe I need to look at the server/db/php settings?

Thanks!

Bootstrap code:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initPlaceholders(){
    Zend_Session::start();

    $this->bootstrap('View');
    $view = $this->getResource('View');

    $view->doctype('XHTML1_STRICT');

    // Set the initial stylesheet:
    $view->headLink()->appendStylesheet('/css/global.css');

    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->registerNamespace('Pog_');


    Zend_Controller_Action_HelperBroker::addPath(
    APPLICATION_PATH . '/controllers/helpers',
        'Application_Controller_Action_Helper_');

}


}

Config file:

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.view[] =
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view.helperPath.View_Helper = APPLICATION_PATH "/views/helpers"
database.adapter         = pdo_mysql
database.params.host     = localhost
database.params.username = user
database.params.password = password
database.params.dbname   = test

DB connection helper:

/**
 * Constructor: initialize plugin loader
 *
 * @return void
 */
public function __construct()
{
    try{

        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
        $dbAdapter = Zend_Db::factory($config->database);
        $dbAdapter->getConnection();
        $this->connection = $dbAdapter;
    } catch (Zend_Db_Adapter_Exception $e) {
        echo 'perhaps a failed login credential, or perhaps the RDBMS is not running';
    } catch (Zend_Exception $e) {
        echo 'perhaps factory() failed to load the specified Adapter class';
    }
}
public function getDbConnection(){
    return $this->connection;
}
}

Index:

// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
        ->run();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坦然微笑 2024-12-17 08:49:25

将数据库定义为资源

resources.db.adapter = pdo_mysql
resources.db.params.host = localhost
resources.db.params.username = user
resources.db.params.password = password
resources.db.params.dbname = test

在主文件中,您只需启动查询即可,无需执行任何操作,而不必担心从您的配置中分配数据库 - 它在内部完成,数据库资源始终被选为您的默认适配器数据库事务

Define your database as a resource

resources.db.adapter = pdo_mysql
resources.db.params.host = localhost
resources.db.params.username = user
resources.db.params.password = password
resources.db.params.dbname = test

In your main files you then need to do nothing but initiate a query without having to worry about assigning the database fvrom your config - its done in the inside, the DB resource is always chosen as the default adapter for your database transactions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文