【问题标题】:how to connect to mysql database in zend framework using connection class如何使用连接类连接到zend框架中的mysql数据库
【发布时间】:2013-08-15 05:14:32
【问题描述】:

我是 Zend 框架的新手。我已经在控制器的操作方法中尝试了每个特定页面上的数据库连接,它工作正常。
我正在使用 WAMP 服务器,但现在我想在一页上学习数据库连接类。并在不同的不同操作方法上使用它。我想在索引页面上建立连接并在项目的所有页面上使用。

这是我在控制器中的操作方法:

 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

【问题讨论】:

    标签: php mysql zend-framework


    【解决方案1】:

    在您的主应用程序引导程序中执行此操作。

    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;
            }
    
    }
    

    应用程序.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 块进行配置。

    【讨论】:

      【解决方案2】:

      你必须像这样编辑 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 并为数据库中的每个表创建一个类。
      • 我添加了这一行并使用它。在我的项目中添加这一行之后。我项目的任何页面都不显示任何内容..在此之前运行正常
      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      相关资源
      最近更新 更多