Zend Framework tutorial on the connection to the database and the implementation of additions and deletions to check the method (with demo source download) _php instance

Source: Internet
Author: User
Tags mysql host zend zend framework

This article is an example of the Zend Framework tutorial to connect to the database and perform additions and deletions to check the method. Share to everyone for your reference, specific as follows:

Let's start by building a table called message in the database, which has three fields. Id,title,content. Where ID is the primary key.

Now we start with the first step: Add a config folder under the Application folder and add a Config.ini file here ... This is the basic configuration database information.

As shown in the following code:

[General]
Db.adapter=pdo_mysql//Please open PDO extension
db.config.host=localhost//mysql host
db.config.username=root
//username db.config.password=//password, I am here for empty
db.config.dbname=zendoophp//database name

Step Two: Add a message.php file under the Models folder under Application. Named here is the same as the data table name.

<?php
Class Message extends Zend_db_table {
protected $_name = ' message ';
Protected $_primary = ' id ';
}

Step three: Next. We are going to add the following code to our entry file index.php:

Configure database parameters and connect to the database 
$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 Fourth: We are going to operate on our indexcontroller.php controller. There are four different methods. Their role is to add data, modify,

Delete data. The procedure is as follows. (I have annotations in programmers, not much here!):

Class Indexcontroller extends Zend_controller_action {function init () {$this->registry = Zend_registry::geti 
  Nstance (); 
  $this->view = $this->registry[' view '; 
 $this->view->baseurl = $this->_request->getbaseurl (); The function indexaction () {$message =new message ()//Instantiate database class//Here assigns a value to the variable, which is displayed in the index.phtml template $this->view->b 
  Odytitle = ' Hello world! '; 
  Take all the data. Two-D array $this->view->messages= $message->fetchall ()->toarray (); 
  Print_r ($this->view->messages); echo $this->view->render (' index.phtml ');//Display template} function Addaction () {//If the value is post. Increase. Otherwise, the page is displayed if (strtol
 Ower ($_server[' request_method ') = = ' Post ') {//filter some data. But there are still tests. Some actions are not done. Please add the ... 
 I don't write that much anymore. Time Relationship: 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 ' You add data success! ' please $this->view->baseurl. ' 
  /index/index/"> Return"; }else{echo $this->view->render (' add.phtml ');//Show Add template}} public Function Editaction () {$message =new Me 
 Ssage (); 
 $db = $message->getadapter (); 
 Zend_loader::loadclass (' zend_filter_striptags '); 
 $filter =new zend_filter_striptags (); Same as above 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);  Update table Data $message->update ($set, $where) unset ($set); Echo ' You modified the data successfully! Please $this->view->baseurl. ' 
 /index/index/"> Return"; }else{$id = $filter->filter (($this->_request->getparam (' id ')); 
  $this->view->messages= $message->fetchall (' id= '. $id)->toarray (); 
 echo $this->view->render (' edit.phtml ');//show Edit templates}} public Function Delaction () {$message =new message (); 
  Can delete data by ID. There are some actions that have not been made. For example, there is no ID page where to go. . I'm just giving you a thought. 
 So it's not so complete $id = (int) $this->_request->getparam (' id '); 
   if ($id > 0) {$where = ' id = '. $id; 
 $message->delete ($where); Echo ' You deleted the data successfully! Please $this->view->baseurl. ' 
 /index/index/"> Return";

 } 
}

The fifth step: to increase the corresponding view. That is, the page template. The difference is add.phtml,edit.phtml,index.phtml. There are also annotations in the program. Please download the file to run the view.

Full instance code click here to download the site.

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.