国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 php教程 PHP開發(fā) Zend Framework教程之連接資料庫並執(zhí)行增刪查的方法

Zend Framework教程之連接資料庫並執(zhí)行增刪查的方法

Jan 05, 2017 am 11:10 AM

本文實例講述了Zend Framework教程之連接資料庫並執(zhí)行增刪查的方法。分享給大家供大家參考,具體如下:

我們先要在資料庫裡建立一個叫message的表,它有三個字段.分別為id,title,content.其中id為主鍵.

現(xiàn)在我們開始第一步:在application資料夾下面加入一個config資料夾,並在這裡面增加一個config.ini檔案..這裡面是設(shè)定資料庫基本資訊.

如下程式碼所示:

[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檔..這裡命名是以資料表名稱相同.

<?php
class Message extends Zend_Db_Table {
protected $_name ="message";
protected $_primary = &#39;id&#39;;
}

第三步:接下來..我們要在我們的入口檔index.php裡加入下面程式碼如下:

//配置數(shù)據(jù)庫參數(shù),并連接數(shù)據(jù)庫 
$config=new Zend_Config_Ini(&#39;./application/config/config.ini&#39;,null, true); 
Zend_Registry::set(&#39;config&#39;,$config); 
$dbAdapter=Zend_Db::factory($config->general->db->adapter,
$config->general->db->config->toArray()); 
$dbAdapter->query(&#39;SET NAMES UTF8&#39;); 
Zend_Db_Table::setDefaultAdapter($dbAdapter); 
Zend_Registry::set(&#39;dbAdapter&#39;,$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[&#39;view&#39;]; 
  $this->view->baseUrl = $this->_request->getBaseUrl(); 
 } 
 function indexAction() 
 {  
  $message=new message();//實例化數(shù)據(jù)庫類 
  //這里給變量賦值,在index.phtml模板里顯示 
  $this->view->bodyTitle = &#39;Hello World!&#39;; 
  //取到所有數(shù)據(jù).二維數(shù)組 
  $this->view->messages=$message->fetchAll()->toArray(); 
  //print_r( $this->view->messages); 
  echo $this->view->render(&#39;index.phtml&#39;);//顯示模版 
 } 
 function addAction(){ 
 //如果是POST過來的值.就增加.否則就顯示增加頁面 
 if(strtolower($_SERVER[&#39;REQUEST_METHOD&#39;])==&#39;post&#39;){ 
 //過濾一些數(shù)據(jù).不過這里還有檢測一些動作沒有做..
 //請大家加了..我就不多寫那么多了.時間關(guān)系.. 
 Zend_Loader::loadClass(&#39;Zend_Filter_StripTags&#39;); 
 $filter=new Zend_Filter_StripTags(); 
 $content=$filter->filter(($this->_request->getPost(&#39;content&#39;))); 
 $title=$filter->filter(($this->_request->getPost(&#39;title&#39;))); 
 $message=new Message(); 
 $data=array( 
 &#39;content&#39;=>$content, 
 &#39;title&#39;=>$title
 );
 $message->insert($data); 
 unset($data); 
 echo &#39;您增加數(shù)據(jù)成功!請您 
 $this->view->baseUrl.&#39;/index/index/">返回&#39;; 
  }else{ 
   echo $this->view->render(&#39;add.phtml&#39;);//顯示增加模版 
  } 
 } 
 public function editAction(){ 
 $message=new Message(); 
 $db = $message->getAdapter(); 
 Zend_Loader::loadClass(&#39;Zend_Filter_StripTags&#39;); 
 $filter=new Zend_Filter_StripTags(); 
 //同上面addAction 
 if(strtolower($_SERVER[&#39;REQUEST_METHOD&#39;])==&#39;post&#39;){ 
 $content=$filter->filter(($this->_request->getPost(&#39;content&#39;))); 
 $title=$filter->filter(($this->_request->getPost(&#39;title&#39;))); 
 $id=$filter->filter(($this->_request->getPost(&#39;id&#39;))); 
  $set=array( 
  &#39;content&#39;=>$content, 
  &#39;title&#39;=>$title
 ); 
 $where = $db->quoteInto(&#39;id = ?&#39;, $id); 
 //更新表數(shù)據(jù) 
 $message->update($set, $where) 
 unset($set);  echo &#39;您修改數(shù)據(jù)成功!請您 
 $this->view->baseUrl.&#39;/index/index/">返回&#39;; 
 }else{ 
  $id=$filter->filter(($this->_request->getParam(&#39;id&#39;))); 
 $this->view->messages=$message->fetchAll(&#39;id=&#39;.$id)->toArray(); 
  echo $this->view->render(&#39;edit.phtml&#39;);//顯示編輯模版 
   } 
 } 
public function delAction() 
{ $message=new Message(); 
 //能過ID刪除數(shù)據(jù).這里有一些動作沒有做.比如說沒有ID頁面要去哪里. 
  //.我只是給大家一個思想..所以不會那么完整 
 $id = (int)$this->_request->getParam(&#39;id&#39;); 
 if ($id > 0) { 
   $where = &#39;id = &#39; . $id; 
   $message->delete($where); 
 } 
 echo &#39;您刪除數(shù)據(jù)成功!請您 
 $this->view->baseUrl.&#39;/index/index/">返回&#39;; 
 } 
}

第五步:就是增加對應(yīng)的View.也就是網(wǎng)頁模板..分別是add.phtml,edit.phtml,index.phtml.

希望本文所述對大家基於Zend Framework框架的PHP程式設(shè)計有所幫助。

更多Zend Framework教程之連接資料庫並執(zhí)行增刪查的方法相關(guān)文章請關(guān)注PHP中文網(wǎng)!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)