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

yipeiwu_com5年前PHP代码库

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
复制代码 代码如下:

<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啦,浏览器显示:
zend framework配置操作数据库

相关文章

apache+php完美解决301重定向的两种方法

幸好有301重定向能有效解决这样的问题。正如月光博客这篇文章中说的, 301重定向可促进搜索引擎优化效果 从搜索引擎优化角度出发,301重定向是网址重定向最为可行的一种办法。当网站的域名...

php文件操作之文件写入字符串、数组的方法分析

php文件操作之文件写入字符串、数组的方法分析

本文实例讲述了php文件操作之文件写入字符串、数组的方法。分享给大家供大家参考,具体如下: 场景一:用文本文档记录一些操作日志,因为对于一些频繁的操作,操作记录的数据量势必会很大,...

php读取文件内容的方法汇总

本文实例汇总了php读取文件内容的方法。分享给大家供大家参考。具体如下: 这里汇总php读取文件内容的五种方法。实际应用当中,请注意关闭 fclose($fp); 第一种方法:fread...

php中关于普通表单多文件上传的处理方法

然而有些情况只需要传递几个文件,而且文件体积并不太大,这种情况下使用组件则有点牛刀杀鸡的感觉,通过html自带的<input type="file">表单就可以实现需要的功能...

ZF等常用php框架中存在的问题

从Zend Framework v0.13版本开始,我就开始学习使用Zend Framework。当时公司的一个项目也恰好用到了Zend Fram...