ZendFramework之如何連接數(shù)據(jù)庫并執(zhí)行增刪查-創(chuàng)新互聯(lián)

這篇文章主要介紹Zend Framework之如何連接數(shù)據(jù)庫并執(zhí)行增刪查,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

為企業(yè)提供網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)站優(yōu)化、全網(wǎng)營銷推廣、競價(jià)托管、品牌運(yùn)營等營銷獲客服務(wù)。創(chuàng)新互聯(lián)擁有網(wǎng)絡(luò)營銷運(yùn)營團(tuán)隊(duì),以豐富的互聯(lián)網(wǎng)營銷經(jīng)驗(yàn)助力企業(yè)精準(zhǔn)獲客,真正落地解決中小企業(yè)營銷獲客難題,做到“讓獲客更簡單”。自創(chuàng)立至今,成功用技術(shù)實(shí)力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營銷”三大難題,同時降低了營銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認(rèn)可!

具體如下:

我們先要在數(shù)據(jù)庫里建立一個叫message的表,它有三個字段.分別為id,title,content.其中id為主鍵.

現(xiàn)在我們開始第一步:在application文件夾下面加入一個config文件夾,并在這里面增加一個config.ini文件..這里面是配置數(shù)據(jù)庫基本信息.

如下代碼所示:

[general]
db.adapter=PDO_MYSQL //請開啟PDO擴(kuò)展
db.config.host=localhost //Mysql主機(jī)
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控制器進(jìn)行操作了..分別有四個方法.它們的作用就是增加數(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í)例化數(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.這在程序里也有注解.

以上是“Zend Framework之如何連接數(shù)據(jù)庫并執(zhí)行增刪查”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前文章:ZendFramework之如何連接數(shù)據(jù)庫并執(zhí)行增刪查-創(chuàng)新互聯(lián)
標(biāo)題URL:http://bm7419.com/article18/iipgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、動態(tài)網(wǎng)站、網(wǎng)站策劃網(wǎng)站排名、建站公司小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司