本文實例講述了Zend Framework教程之連接資料庫並執行增刪查的方法。分享給大家供大家參考,具體如下:
我們先要在資料庫裡建立一個叫message的表,它有三個字段.分別為id,title,content.其中id為主鍵.
現在我們開始第一步:在application資料夾下面加入一個config資料夾,並在這裡面增加一個config.ini檔案..這裡面是設定資料庫基本資訊.
如下程式碼所示:
[general] db.adapter=PDO_MYSQL //请开启PDO扩展 db.config.host=localhost //Mysql主机 db.config.username=root //用户名 db.config.password= //密码,我这里为空 db.config.dbname=zendoophp //数据库名
第二步:在application下的models資料夾下增加一個Message.php檔..這裡命名是以資料表名稱相同.
<?php class Message extends Zend_Db_Table { protected $_name ="message"; protected $_primary = 'id'; }
第三步:接下來..我們要在我們的入口檔index.php裡加入下面程式碼如下:
//配置数据库参数,并连接数据库 $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控制器進行操作了..分別有四個方法.它們的作用就是增加數據,修改,
刪除數據.程序如下..(我在程序員都有註解.這裡不就多說!):
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();//实例化数据库类 //这里给变量赋值,在index.phtml模板里显示 $this->view->bodyTitle = 'Hello World!'; //取到所有数据.二维数组 $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'){ //过滤一些数据.不过这里还有检测一些动作没有做.. //请大家加了..我就不多写那么多了.时间关系.. 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 '您增加数据成功!请您 $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); //更新表数据 $message->update($set, $where) unset($set); echo '您修改数据成功!请您 $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删除数据.这里有一些动作没有做.比如说没有ID页面要去哪里. //.我只是给大家一个思想..所以不会那么完整 $id = (int)$this->_request->getParam('id'); if ($id > 0) { $where = 'id = ' . $id; $message->delete($where); } echo '您删除数据成功!请您 $this->view->baseUrl.'/index/index/">返回'; } }
第五步:就是增加對應的View.也就是網頁模板..分別是add.phtml,edit.phtml,index.phtml.
希望本文所述對大家基於Zend Framework框架的PHP程式設計有所幫助。
更多Zend Framework教程之連接資料庫並執行增刪查的方法相關文章請關注PHP中文網!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版