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

相关文章

不用mod_rewrite直接用php实现伪静态化页面代码

在你的程序初始化时使用如下代码: 复制代码 代码如下:<?php $Php2Html_FileUrl = $_SERVER["REQUEST_URI"]; $Php2Ht...

PHP基于数组实现的分页函数实例

分页功能是PHP程序设计中非常常见的功能,不同于以往的,今天本文介绍的是PHP基于数组实现的分页函数。 关于数组的分页函数,用数组进行分页的好处是可以方便的进行联合多表查询,只需要将查询...

PHP中Date()时间日期函数的使用方法小结

语法 date(format,timestamp)参数 描述 format 必需。规定时间戳的格式。 timestamp 可选。规定时间戳。默认是当前的日期和时间 要找出前一天的时间就是...

php+html5+ajax实现上传图片的方法

本文实例讲述了php+html5+ajax实现上传图片的方法。分享给大家供大家参考,具体如下: <?php if (isset($_POST['upload'])) {...

php获取目录中所有文件名及判断文件与目录的简单方法

一,php获取目录中的所有文件名 1、打开要操作目录的目录句柄 代码示例: //打开当前目录下的目录pic下的子目录common。 $handler = opendir('pic/c...