


Zend Framework tutorial: How to connect to the database and perform addition and deletion queries
Jan 05, 2017 am 11:10 AMThe example in this article describes the method of connecting to the database and performing addition, deletion and query in the Zend Framework tutorial. Share it with everyone for your reference, the details are as follows:
We first need to create a table called message in the database, which has three fields. They are id, title, and content. Among them, id is the primary key.
Now we start the first step: add a config folder under the application folder, and add a config.ini file in it.. This contains the basic information of the configuration database.
The following code Display:
[general] db.adapter=PDO_MYSQL //請開啟PDO擴展 db.config.host=localhost //Mysql主機 db.config.username=root //用戶名 db.config.password= //密碼,我這里為空 db.config.dbname=zendoophp //數(shù)據(jù)庫名
The second step: Add a Message.php file under the models folder under the application. The name here is the same as the name of the data table.
<?php class Message extends Zend_Db_Table { protected $_name ="message"; protected $_primary = 'id'; }
The third step: Connect Come on. We need to add the following code to our entry file index.php as follows:
//配置數(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);
Step 4: We are about to operate our IndexController.php controller. There are four of them. Methods. Their function is to add data, modify, and
delete data. The program is as follows.. (I have notes in the programmer. I won’t say more here!):
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/">返回'; } }
Fifth Step: Add the corresponding View. That is, the web page template.. They are add.phtml, edit.phtml, index.phtml.
I hope this article will be helpful to everyone’s PHP program design based on the Zend Framework framework. help.
For more Zend Framework tutorials on how to connect to a database and perform add/delete queries, please pay attention to the PHP Chinese website for related articles!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
