Zend Framework教程之串連資料庫並執行增刪查的方法(附demo源碼下載),zenddemo_PHP教程

來源:互聯網
上載者:User

Zend Framework教程之串連資料庫並執行增刪查的方法(附demo源碼下載),zenddemo


本文執行個體講述了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檔案..這裡命名是以資料表名稱相同.

<?phpclass 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相關內容感興趣的讀者可查看本站專題:《Zend FrameWork架構入門教程》、《php優秀開發架構總結》、《Yii架構入門及常用技巧總結》、《ThinkPHP入門教程》、《php物件導向程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於Zend Framework架構的PHP程式設計有所協助。

您可能感興趣的文章:

  • Zend Framework架構教程之Zend_Db_Table_Rowset用法執行個體分析
  • Zend Framework教程之Zend_Db_Table_Row用法執行個體分析
  • Zend Framework教程之Zend_Db_Table用法詳解
  • Zend Framework教程之Zend_Form組件實現表單提交並顯示錯誤提示的方法
  • Zend Framework開發入門經典教程
  • Zend Framework架構Smarty擴充實現方法
  • Zend Framework架構路由機制程式碼分析
  • Zend Framework實現具有準系統的留言本(附demo源碼下載)
  • Zend Framework實現將session儲存在memcache中的方法
  • Zend Framework分頁類用法詳解
  • Zend Framework實現多檔案上傳功能執行個體
  • Zend Framework入門之環境配置及第一個Hello World樣本(附demo源碼下載)
  • Zend Framework教程之Zend_Db_Table表關聯執行個體詳解

http://www.bkjia.com/PHPjc/1113739.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1113739.htmlTechArticleZend Framework教程之串連資料庫並執行增刪查的方法(附demo源碼下載),zenddemo 本文執行個體講述了Zend Framework教程之串連資料庫並執行增刪查的方法...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.