zend framework配置操作數(shù)據(jù)庫實例分析
zendframework項目環(huán)境搭建后,看了下zend framework配置操作數(shù)據(jù)庫,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數(shù)據(jù)庫來測試下,就用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 2.0事件管理器(The EventManager)入門教程
- Zend Framework數(shù)據(jù)庫操作技巧總結(jié)
- Zend Framework數(shù)據(jù)庫操作方法實例總結(jié)
- Zend Framework入門教程之Zend_Db數(shù)據(jù)庫操作詳解
- ZendFramework框架實現(xiàn)連接兩個或多個數(shù)據(jù)庫的方法
- Zend Framework教程之連接數(shù)據(jù)庫并執(zhí)行增刪查的方法(附demo源碼下載)
- Zend Framework連接Mysql數(shù)據(jù)庫實例分析
- 解析如何使用Zend Framework 連接數(shù)據(jù)庫
- windows下zendframework項目環(huán)境搭建(通過命令行配置)
- zend framework多模塊多布局配置
- Zend Framework開發(fā)入門經(jīng)典教程
- ZendFramework2連接數(shù)據(jù)庫操作實例
相關(guān)文章
PHP異常Parse error: syntax error, unexpected T_VAR錯誤解決方法
在PHP中根本不需要使用var聲明的,但是當(dāng)一個變量作為一個類的成員變量的時候,使用var還是沒有問題的2014-05-05linux下使用ThinkPHP需要注意大小寫導(dǎo)致的問題
今天把剛完成的第一部分功能部署到客戶的測試服務(wù)器上,結(jié)果傻眼了,好多功能都用不了,列表頁刷出來全是空的。2011-08-08php中實現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法
這篇文章主要介紹了php中實現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法,實例封裝了一個類文件,可實現(xiàn)XML與MySQL數(shù)據(jù)的相互轉(zhuǎn)換,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12