简体   繁体   中英

How to configure MySQL as the adapter in Zend DB Adapter

I have read all the documentation on using database adapters with Zend and I completely understand it theoritically but am completely lost as to how to set it up. I am trying to follow the instructions KarmicDice left on this post but am having a lot of trouble with configuring MySql with my Zend Db Adapter. Can someone please give me step by step instructions on what to do? I cannot find any accurate resources on this.

First, I will shamelessly copy-paste the config from the post you linked:

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
resources.db.adapter = "PDO_MYSQL"
resources.db.params.dbname = "<your database name>"
resources.db.params.host = "<your host name>"
resources.db.params.username = "<mysql username>"
resources.db.params.password = "<mysql password>"
resources.db.params.charset  = "UTF8"

This really is all you need in order to configure the adapter.

In ZF there are many, MANY ways you can set up your project to interact with the database, for example setting up a DataMapper or ActiveRecord pattern. But for a very simple example to get you up and running with the connection, I'm just going to dump some code straight into a controller (don't let your kids watch this!)

<?php

class IndexController extends Zend_Controller_Action {

    public function indexAction() {
        $db = Zend_Db_Table::getDefaultAdapter();
        $query = $db->select()->from('users', array('email')); // SELECT `users`.`email` FROM `users`
        $result = $db->fetchAll($query);
        var_dump($result);
    }

}

These two code snippets are all you should need to dump a list of all the users' email addresses (assuming the connection info and table name is right). If you need help setting up files for a model/mapper system, this link has very detailed code you can pretty much just copy-paste into your project.

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