zend application.php,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

class Wp_posts extends Zend_Db_Table{

protected $_name = 'Wp_posts';

protected $_primary = 'ID';

}

?>

控制器controller下面建立IndexController.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

<?php foreach ($this->res as $news){?>

<?php echo $news['id']?><?php echo $news['post_title']?><?php echo $news['post_date']?>

ok啦,浏览器显示:

1-200120161P5959.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值