Use of ZF Database

Source: Internet
Author: User
Tags db2 ibm db2 zend framework

Today we will teach you how to use Zend Framework to connect to the database. Zend_Db provides two methods to connect to the database: 1. Zend_Db, 2. Zend_Db_Table.
Zend_Db is a relatively simple DRBMS provided by Zend_Db:
IBM DB2 (pdo_ibm)
IBM DB2 and Informix Dynamic Server (IDS) (pdo_ibm)
MySQL (pdo_mysql)
MySQLi (mysqli)
Microsoft SQL (pdo_mssql)
Oracle (pdo_oci)
PostgreSQL (pdo_pgsql)
SQLite (pdo_sqlite)

You can find the information http://framework.zend.com/apidoc/core/ of Zend_Db class here

Operating Process:
1. Create the config. ini (host, database, user, password... etc) file of the Connection database)
2. Create an interface to connect to the database
3. Start to connect to the database
4. receive the information and upload it back to the View (page)

Create a config directory under the application directory, and then create the config. ini file in the config directory.

Config. ini
Code:

[General]
Db. adapter = mysqli // change it to your RDBMS
Db. config. host = localhost // database location
DB. config. Username = username // username used to connect to the database
DB. config. Password = PASSWORD // password used to connect to the database
DB. config. dbname = database // Database Name

Index. php
Code:

<? PHP

Set_include_path ('.'. path_separator. './library/'. path_separator. './application/m odels/'); // set the module location
Include "Zend/loader. php"; // the module that must be included. It imports the required Module

Zend_loader: loadclass ('zend _ controller_front '); // zend_controller_front is a required module to control the route
Zend_loader: loadclass ('zend _ db ');
Zend_loader: loadclass ('zend _ config_ini ');
Zend_loader: loadclass ('zend _ registry ');

$ Config = new zend_config_ini ('./application/config. ini', 'General '); // import the config file of the database

// Set the Database Interface
$ Dbadapter = zend_db: Factory ($ config-> DB-> adapter, $ config-> DB-> config-> toarray ());
Zend_registry: Set ('dbadapter ', $ dbadapter );

$ Frontcontroller = zend_controller_front: getinstance (); // create a front object
$ Frontcontroller-> setcontrollerdirectory ('./application/controllers'); // set the path of the controller directory
$ Frontcontroller-> dispatch ();

Indexcontroller. php
Code:

<? PHP

Class indexcontroller extends zend_controller_action
{
Public Function indexaction ()
{
Try {
$ Db = zend_registry: Get ('dbadapter '); // introduce the Database Interface
$ Select = $ db-> select ();
$ Select-> from ('bank', '*') // or ('database', array ('id', 'username'... etc)
-> Where ('Bank _ code! =? ', 'Bbb ')
-> Order ('Bank _ Code ');

$ Result = $ db-> fetchAll ($ select); // The result is returned as an Array.
$ This-> view-> data = $ result;
}
Catch (Zend_Exception $ e ){
Echo "Error:". $ e-> getMessage ();
}
}
}

Index. phtml
Code:

<? Php
Foreach ($ this-> data as $ key => $ value ):
Echo "bankcode =". $ value ['Bank _ Code']. ": bank =". $ value ['bank']. "<br> ";
Endforeach;
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.