如何使用连接类在Zend框架中连接到MySQL数据库

3

我是Zend框架的新手。我已经尝试在控制器的每个特定页面的action方法中进行数据库连接,它运行良好。
我正在使用WAMP服务器,但现在我想要学习如何在一个页面上创建数据库连接类,并在不同的action方法中使用它。我想在index页面上建立连接,并在整个项目的所有页面中使用。

以下是我在控制器中的action方法:

 public function userAction()
    {

        $db = Zend_Db_Table::getDefaultAdapter();
            $data = array(
                 'first_name' => 'xyz',
                 'last_name' => 'xyz',
                 'user_name' => 'xyz',
                 'password' => 'xyz'
                  );
           $rows_affected = $db->insert('user', $data);
           $last_insert_id = $db->lastInsertId();

    }

以下是应用程序配置文件 application.ini,我只在文件中添加了数据库适配器设置。

    [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.db.adapter = "PDO_MYSQL"//adapter
    resources.db.params.host ="localhost" //server name here or host
    resources.db.params.username = "root"///username here
    resources.db.params.password = "" //database password
    resources.db.params.dbname = "zend"//database name
    resources.db.isDefaultTableAdapter = true

    [staging : production]

    [testing : production]
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1

    [development : production]
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1
    resources.frontController.params.displayExceptions = 1
2个回答

2
在您的主应用程序引导程序中执行以下操作。
protected function _initMysql() {
    $this->bootstrap('db');
        switch (APPLICATION_ENV) {

            case 'development' :
                // this allows you to profile your queries through the firebug console 
                $profiler = new Zend_Db_Profiler_Firebug('System Queries');
                $profiler->setEnabled(true);
                $this->getPluginResource('db')->getDbAdapter()->setProfiler($profiler);
                break;

            case 'production' :
                // if you use meta caching in production, which you should :)
                // Zend_Db_Table_Abstract::setDefaultMetadataCache($this->_cache);
                break;
        }

}

application.ini

resources.db.adapter = "Pdo_Mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "*****"
resources.db.params.password = "*****"
resources.db.params.dbname = "******"
resources.db.driver_options.charset = "utf-8"
resources.db.isDefaultTableAdapter = true

当然,确保在index.php中传递正确的APPLICATION_ENV,因为它确定应用程序将使用哪个application.ini块进行配置。


0

你需要像这样编辑application.ini文件。

添加以下代码。

resources.db.adapter = //adapter
resources.db.params.host = //server name here or host
resources.db.params.username = ///username here
resources.db.params.password =  //database password
resources.db.params.dbname = //database name

如果您提供所需的数据,则现在已连接到数据库。您必须启用DBtable并为数据库中的每个表创建一个类。 - shark
我添加了这行代码并使用它。在我的项目中添加了这行代码后,任何页面都无法显示,而在此之前它们都能正常运行。 - unnati

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接