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

yipeiwu_com6年前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 mkdir()定义和用法

使用方法: mkdir(path,mode,recursive,context) 参数 描述 path 必需。规定要创建的目录的名称。 mode 必需。规定权限。默认是 0777。 re...

PHP实现的简单四则运算计算器功能示例

本文实例讲述了PHP实现的简单四则运算计算器功能。分享给大家供大家参考,具体如下: php实现一个简单的四则运算计算器(还不支持括号的优先级)。利用栈这种数据结构来计算表达式很赞。 这里...

php连接odbc数据源并保存与查询数据的方法

本文实例讲述了php连接odbc数据源并保存与查询数据的方法。分享给大家供大家参考。 具体实现代码如下: 复制代码 代码如下: $connstr = "driver=microsoft...

PHP中去除换行解决办法小结(PHP_EOL)

第一种写法: $content=str_replace("\n","",$content); echo $content; 第二种写法: str_replace("\r\n","",$s...

关于zend studio 出现乱码问题的总结

出现乱码的地方大概有4个地方:1、文件的编码方式(就是你新建文件的编码),这一点需要设置编辑器的编码方式。2、页面没有指定浏览器编码的显示方式,这一点解决的办法是:1,如果页面是.htm...