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判斷文件是否被引入的方法get_included_files用法示例
這篇文章主要介紹了PHP判斷文件是否被引入的方法get_included_files用法,結(jié)合實例形式分析了get_included_files函數(shù)獲取引入文件及遍歷輸出的操作技巧,需要的朋友可以參考下2016-11-11
php字符串截取函數(shù)mb_substr用法實例分析
這篇文章主要介紹了php字符串截取函數(shù)mb_substr用法,結(jié)合實例形式分析了php使用mb_substr針對中文字符串截取與編碼控制相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
php中convert_uuencode()與convert_uuencode函數(shù)用法實例
這篇文章主要介紹了php中convert_uuencode()與convert_uuencode函數(shù)用法,以實例形式了convert_uuencode()與convert_uuencode進行編碼與解碼的方法,是非常實用的技巧,需要的朋友可以參考下2014-11-11
PHP中array_map與array_column之間的關(guān)系分析
這篇文章主要介紹了PHP中array_map與array_column之間的關(guān)系分析,對于PHP初學(xué)者來說是比較實用的概念,需要的朋友可以參考下2014-08-08
php學(xué)習(xí)筆記之字符串常見操作總結(jié)
這篇文章主要介紹了php學(xué)習(xí)筆記之字符串常見操作,結(jié)合實例形式總結(jié)分析了php字符串的定義、單引號與雙引號的用法以及常見字符串操作函數(shù)使用技巧,需要的朋友可以參考下2019-07-07

