zend framework配置操作数据库实例分析

zendframework项目环境搭建后,看了下zend framework配置操作数据库PHP教程如下:
在application/configs的文件下建立一个config.ini文件

配置信息如下


[general]
db.adapter=PDO_MysqL
db.config.host=localhost/IParess
db.config.username=username
db.config.password=password
db.config.dbname=databasename
2、
在pulibc 目录的index.PHP页面
/** Zend_Application */
require_once 'Zend/Application.PHP';
的下面插入
//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::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter',$dbAdapter);
就此,我就用我的本地wordpress数据库来测试下,就用wp_posts表来测试吧:
首先模型models建立Wp_posts.PHP
代码如下:
PHP
class Wp_posts extends Zend_Db_Table{
protected $_name = 'Wp_posts';
protected $_primary = 'ID';
}
?>

控制器controller下面建立IndexController.PHP
代码如下:
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");
}
}

在views/scripts/index/ 建立视图:index.phtml
代码如下:


this is for test


res as $news){?>




ok啦,浏览器显示

数据库" style="disPLAY: block; MARGIN-LEFT: auto; MARGIN-RIGHT: auto" alt="zend framework配置操作数据库" src="https://files.jb51.cc/file_images/article/201212/2012120611435830.png">

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...