简体   繁体   中英

how to connect to mysql database in zend framework using connection class

I am new to Zend framework.I have tried database connection on each particular page in action method of controller its working fine.
I am using WAMP server, but now I want to learn database connection class on one page.and using that on different different action method. I want a connection made on index page and using at the all pages of project.

This is my action method in controller:

 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();

    }

and application.ini file is below in which i add only database adapter setting in this 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.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

In your main application bootstrap do this.

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

And of course make sure you are passing the correct APPLICATION_ENV in index.php as that determines which application.ini block the application will use for its configuration.

You have to edit the application.ini like this.

add this code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM