源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-08-18 05:55 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:zend framework配置操作数据库实例分析
[url=http://www.1sucai.cn/article/32485.htm]zendframework项目环境搭建[/url]后,看了下zend framework配置操作数据库,php教程如下: 在application/configs的文件下建立一个config.ini文件 [b]配置信息如下[/b]: [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
[u]复制代码[/u] 代码如下:
<?php class Wp_posts extends Zend_Db_Table{ protected $_name = 'Wp_posts'; protected $_primary = 'ID'; } ?>
控制器controller下面建立IndexController.php
[u]复制代码[/u] 代码如下:
<?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
[u]复制代码[/u] 代码如下:
<html> <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啦,浏览器显示: [img]http://files.jb51.net/file_images/article/201212/2012120611435830.png[/img]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部