Zend Framework教程之連接數(shù)據(jù)庫并執(zhí)行增刪查的方法(附demo源碼下載)
本文實例講述了Zend Framework教程之連接數(shù)據(jù)庫并執(zhí)行增刪查的方法。分享給大家供大家參考,具體如下:
我們先要在數(shù)據(jù)庫里建立一個叫message的表,它有三個字段.分別為id,title,content.其中id為主鍵.
現(xiàn)在我們開始第一步:在application文件夾下面加入一個config文件夾,并在這里面增加一個config.ini文件..這里面是配置數(shù)據(jù)庫基本信息.
如下代碼所示:
[general] db.adapter=PDO_MYSQL //請開啟PDO擴展 db.config.host=localhost //Mysql主機 db.config.username=root //用戶名 db.config.password= //密碼,我這里為空 db.config.dbname=zendoophp //數(shù)據(jù)庫名
第二步:在application下的models文件夾下增加一個Message.php文件..這里命名是以數(shù)據(jù)表名稱相同.
<?php class Message extends Zend_Db_Table { protected $_name ="message"; protected $_primary = 'id'; }
第三步:接下來..我們要在我們的入口文件index.php里加入下面代碼如下:
//配置數(shù)據(jù)庫參數(shù),并連接數(shù)據(jù)庫 $config=new Zend_Config_Ini('./application/config/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);
第四步:我們就要對我們的IndexController.php控制器進行操作了..分別有四個方法.它們的作用就是增加數(shù)據(jù),修改,
刪除數(shù)據(jù).程序如下..(我在程序員都有注解.這里不就多說!):
class IndexController extends Zend_Controller_Action { function init() { $this->registry = Zend_Registry::getInstance(); $this->view = $this->registry['view']; $this->view->baseUrl = $this->_request->getBaseUrl(); } function indexAction() { $message=new message();//實例化數(shù)據(jù)庫類 //這里給變量賦值,在index.phtml模板里顯示 $this->view->bodyTitle = 'Hello World!'; //取到所有數(shù)據(jù).二維數(shù)組 $this->view->messages=$message->fetchAll()->toArray(); //print_r( $this->view->messages); echo $this->view->render('index.phtml');//顯示模版 } function addAction(){ //如果是POST過來的值.就增加.否則就顯示增加頁面 if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ //過濾一些數(shù)據(jù).不過這里還有檢測一些動作沒有做.. //請大家加了..我就不多寫那么多了.時間關(guān)系.. Zend_Loader::loadClass('Zend_Filter_StripTags'); $filter=new Zend_Filter_StripTags(); $content=$filter->filter(($this->_request->getPost('content'))); $title=$filter->filter(($this->_request->getPost('title'))); $message=new Message(); $data=array( 'content'=>$content, 'title'=>$title ); $message->insert($data); unset($data); echo '您增加數(shù)據(jù)成功!請您 $this->view->baseUrl.'/index/index/">返回'; }else{ echo $this->view->render('add.phtml');//顯示增加模版 } } public function editAction(){ $message=new Message(); $db = $message->getAdapter(); Zend_Loader::loadClass('Zend_Filter_StripTags'); $filter=new Zend_Filter_StripTags(); //同上面addAction if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ $content=$filter->filter(($this->_request->getPost('content'))); $title=$filter->filter(($this->_request->getPost('title'))); $id=$filter->filter(($this->_request->getPost('id'))); $set=array( 'content'=>$content, 'title'=>$title ); $where = $db->quoteInto('id = ?', $id); //更新表數(shù)據(jù) $message->update($set, $where) unset($set); echo '您修改數(shù)據(jù)成功!請您 $this->view->baseUrl.'/index/index/">返回'; }else{ $id=$filter->filter(($this->_request->getParam('id'))); $this->view->messages=$message->fetchAll('id='.$id)->toArray(); echo $this->view->render('edit.phtml');//顯示編輯模版 } } public function delAction() { $message=new Message(); //能過ID刪除數(shù)據(jù).這里有一些動作沒有做.比如說沒有ID頁面要去哪里. //.我只是給大家一個思想..所以不會那么完整 $id = (int)$this->_request->getParam('id'); if ($id > 0) { $where = 'id = ' . $id; $message->delete($where); } echo '您刪除數(shù)據(jù)成功!請您 $this->view->baseUrl.'/index/index/">返回'; } }
第五步:就是增加對應(yīng)的View.也就是網(wǎng)頁模板..分別是add.phtml,edit.phtml,index.phtml.這在程序里也有注解.請大家下載文件運行查看.
完整實例代碼點擊此處本站下載。
更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計有所幫助。
- 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連接Mysql數(shù)據(jù)庫實例分析
- 解析如何使用Zend Framework 連接數(shù)據(jù)庫
- zend framework配置操作數(shù)據(jù)庫實例分析
- windows下zendframework項目環(huán)境搭建(通過命令行配置)
- zend framework多模塊多布局配置
- Zend Framework開發(fā)入門經(jīng)典教程
- ZendFramework2連接數(shù)據(jù)庫操作實例
相關(guān)文章
php使用lua+redis實現(xiàn)限流,計數(shù)器模式,令牌桶模式
這篇文章主要介紹了php使用lua+redis實現(xiàn)限流,計數(shù)器模式,令牌桶模式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04WAMP環(huán)境中擴展oracle函數(shù)庫(oci)
本文給大家介紹的是在windows環(huán)境下為php環(huán)境擴展Oracle函數(shù)庫的過程,十分的詳細,有需要的小伙伴可以參考下。2015-06-06PHP實現(xiàn)微信提現(xiàn)(企業(yè)付款到零錢)
這篇文章主要為大家詳細介紹了PHP實現(xiàn)微信提現(xiàn),企業(yè)付款到零錢,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08thinkphp 5框架實現(xiàn)登陸,登出及session登陸狀態(tài)檢測功能示例
這篇文章主要介紹了thinkphp 5框架實現(xiàn)登陸,登出及session登陸狀態(tài)檢測功能,結(jié)合實例形式分析了thinkPHP5登陸判斷、跳轉(zhuǎn)及session的相關(guān)使用技巧,需要的朋友可以參考下2019-10-1050個優(yōu)秀經(jīng)典PHP算法大集合 附源碼
這篇文章主要介紹了50個優(yōu)秀經(jīng)典PHP算法大集合 附源碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08php循環(huán)table實現(xiàn)一行兩列顯示的方法
這篇文章主要介紹了php循環(huán)table實現(xiàn)一行兩列顯示的方法,本文直接給出實現(xiàn)代碼,重點就是在取余方法的運用,需要的朋友可以參考下2015-06-06