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配置操作数据库

相关文章

PHP mail()函数使用及配置方法

配置 工欲善其事,必先利其器。首先我们以windows下面为例进行说明,如何配置一下本地的mail。 下载附件 sendmail.zip -解压到任意路径,修改sendmail...

关于PHP自动判断字符集并转码的详解

原理很简单,因为gb2312/gbk是中文两字节,这两个字节是有取值范围的,而utf-8中汉字是三字节,同样每个字节也有取值范围。而英文不 管在何种编码情况下,都是小于128,只占用一个...

PHP获取文件相对路径的方法

本文实例讲述了PHP获取文件相对路径的方法。分享给大家供大家参考。具体实现方法如下: <?php $a = '/a/b/c/d/e.php'; $b = '/a/b/1...

php输出金字塔的2种实现方法

本文实例讲述了php输出金字塔的2种实现方法。分享给大家供大家参考。具体分析如下: 下面给大家总结了两种实现金字塔打印的方法,一种是利用了自定义函数,另一种是利用了for循环了,其实两都...

使用XHGui来测试PHP性能的教程

使用XHGui来测试PHP性能的教程

Profiling是一项用来观察程序性能的技术,非常适用于发现程序的瓶颈或者紧张的资源。Profiling能够深入程序的内部,展现request处理过程中每一部分代码的性能;同时,也可以...