After the zendframework project is created, I have read the zendframework configuration operation Database. This article will introduce it in detail. if you want to know more, refer to the zendframework project environment and check the zend framework configuration operation database, the php Tutorial is as follows:
Create a config. ini file under the application/configs file
The configuration information is as follows::
[General]
Db. adapter = PDO_MYSQL
Db. config. host = localhost/IParess
Db. config. username = username
Db. config. password = password
Db. config. dbname = databasename
2,
On the index. php page of the pulibc Directory
/** Zend_Application */
Require_once 'zend/Application. php ';
Insert
// Set the datase config
Require_once 'zend/Config/Ini. php ';
Require_once 'zend/Registry. php ';
Require_once 'zend/Db. php ';
Require_once 'zend/Db/Table. php ';
$ Config = new Zend_Config_Ini ('../application/configs/config. ini', null, true );
Zend_Registry: set ('config', $ config );
$ DbAdapter = Zend_Db: factory ($ config-> general-> db-> adapter, $ config-> general-> db-> config-> toArray ());
$ DbAdapter-> query ('set NAMES utf8 ');
Zend_Db_Table: setdefaadapter adapter ($ dbAdapter );
Zend_Registry: set ('dbadapter ', $ dbAdapter );
In this case, I will use my local wordpress database for testing and use the wp_posts table for testing:
First, model models creates Wp_posts.php
The code is as follows:
Class Wp_posts extends Zend_Db_Table {
Protected $ _ name = 'WP _ posts ';
Protected $ _ primary = 'id ';
}
?>
Create IndexController. php under controller
The code is as follows:
Require_once APPLICATION_PATH. '/models/Wp_posts.php ';
Class IndexController extends Zend_Controller_Action
{
Public function init ()
{
/* Initialize action controller here */
}
Public function indexAction ()
{
$ Con = new Wp_posts ();
$ Res = $ con-> fetchAll ()-> toArray ();
$ This-> view-> res = $ res;
$ This-> render ("index ");
}
}
Create a view in views/scripts/index/: index. phtml
The code is as follows:
This is for test
OK. the browser displays: