Zend Framework tutorial: How to connect to the database and perform addition and deletion queries (with demo source code download), zenddemo_PHP tutorial

WBOY
Release: 2016-07-12 08:56:06
Original
778 people have browsed it

Zend Framework tutorial: How to connect to the database and perform add/delete queries (with demo source code download), zenddemo

This article describes the Zend Framework tutorial on how to connect to the database and perform add/delete queries method. 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 is shown:

[general]
db.adapter=PDO_MYSQL //请开启PDO扩展
db.config.host=localhost //Mysql主机
db.config.username=root //用户名
db.config.password= //密码,我这里为空
db.config.dbname=zendoophp //数据库名

Copy after login

Step 2: 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.

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

Copy after login

Step 3: Next.. We need to add the following code to our entry file index.php as follows:

//配置数据库参数,并连接数据库 
$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);

Copy after login

Step 4: We are about to operate our IndexController.php controller. There are four methods. Their function is to add data, modify,

Delete data. The procedure 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();//实例化数据库类 
  //这里给变量赋值,在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 = &#63;', $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/">返回'; 
 } 
}

Copy after login

The fifth step: add the corresponding View. That is, the web page template.. They are add.phtml, edit.phtml, index.phtml. This is also annotated in the program. Please download the file and run it to view.

Click here to download the complete example code from this website.

Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.

Articles you may be interested in:

  • Zend Framework Tutorial - Zend_Db_Table_Rowset Usage Example Analysis
  • Zend Framework Tutorial - Zend_Db_Table_Row Usage Example Analysis
  • Zend Framework Tutorial: Detailed explanation of Zend_Db_Table usage
  • Zend Framework tutorial: Zend_Form component to implement form submission and display error prompts
  • Zend Framework development introduction classic tutorial
  • Zend Framework Smarty extension implementation Method
  • Zend Framework routing mechanism code analysis
  • Zend Framework implements a guestbook with basic functions (with demo source code download)
  • Zend Framework implements session storage in memcache Method
  • Detailed explanation of usage of Zend Framework paging class
  • Zend Framework implementation of multi-file upload function example
  • Environment configuration for getting started with Zend Framework and the first Hello World example (with demo Source code download)
  • Zend Framework tutorial Zend_Db_Table table association example detailed explanation

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113739.htmlTechArticleZend Framework tutorial method of connecting to the database and performing addition and deletion checks (with demo source code download), zenddemo This article describes the example Zend Framework tutorial: How to connect to the database and perform add/delete queries...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template