DB Connection using Zend_Db

intro
In this article, we will connect to DB and execute SQL using Zend_DB. You can download the source code of this article from here.

1.Create config.ini
Create the file application/config.ini with the following content.
Please modify database.params.* properly.

In this article, we will use the following sql.
2.Create Controller
Create the file application/constorllers/IndexController.php with the following content.
3.Create View
Create the file application/views/scripts/index/index.phtml with the following content.

4.Check
Let’s access the Web Server and check the screen.

2 Comments

  1. Is there a way to get the dbadapter object in the IndexController.php from the bootstrap.php file ?
    For example, with:
    $config = new Zend_Config_Ini(CONFIG_PATH, ‘staging’);
    $dbAdapter = Zend_Db::factory($config->database);
    in the bootstrap file, how can i do ?

    Hope i am clear!
    Thx for your great job, it’s a amazing that there is no comment in your articles !

    Nicolas

  2. Thank you for your comment, Nicolas.
    How about using Zend_Registry?
    For example :
    $dbAdapter = Zend_Db::factory($config->database);
    Zend_Registry::set(‘my_db’, $dbAdapter);
    in the bootstrap file, and
    $dbAdapter = Zend_Registry::get(‘my_db’);
    in the controller.

    Detailed information is on
    Programmer’s Reference Guide(10.5.3)

    > Thx for your great job, it’s a amazing that there is no comment in your articles !
    This site just started, and I also started the study of Zend Framework recently.
    So I wish to get more skill together!

Leave a Reply

Your email address will not be published.