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中使用__autoload()自动加载未定义类的实现代码

下面是一段使用__autoload()的代码,供大家学习参考: 复制代码 代码如下:<?php/*** 自动加载相关类库文件*/function __autoload($class...

php如何实现只替换一次或N次

 我们都知道,在PHP里Strtr,strreplace等函数都可以用来替换,不过他们每次替换的时候都是全部替换,举个例子: "abcabbc",这个字符串如果使用上边的函数来...

对象失去焦点时自己动提交数据的实现代码

对象失去焦点时自己动提交数据的实现代码

解决这个问题,得需要使用onblur来实现。下面代码并非是专案实现代码,只是模拟相同的功能。 复制代码 代码如下: <!--Ajax实现页面不闪烁,一直是Insus.NET所喜欢使...

php 分页原理详解

在看本文之前,请确保你已掌握了PHP的一些知识以及MYSQL的查询操作基础哦。 作为一个Web程序,经常要和不计其数的数据打交道,比如会员的数据,文章数据,假如只有几十个会员那很好办,...

php 将bmp图片转为jpg等其他任意格式的图片

复制代码 代码如下:<? php // 例子: $path = ROOT . ' upload/2009/06/03/124401282315771. ' ; $pathAll =...