After the zendframework project environment is set up, 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
Copy codeThe Code is as follows: <? Php
Class Wp_posts extends Zend_Db_Table {
Protected $ _ name = 'wp _ posts ';
Protected $ _ primary = 'id ';
}
?>
Create IndexController. php under controllerCopy codeThe Code is as follows: <? Php
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. phtmlCopy codeThe Code is as follows: <Head>
<Title> this is for test </title>
</Head>
<Body>
<Table>
<? Php foreach ($ this-> res as $ news) {?>
<Tr>
<Td> <? Php echo $ news ['id']?> </Td>
<Td> <? Php echo $ news ['Post _ title']?> </Td>
<Td> <? Php echo $ news ['Post _ date']?> </Td>
</Tr>
<? Php }?>
</Table>
</Body>
</Html>
OK. The browser displays: