The ZendFramework tutorial describes how to connect to the database and perform addition, deletion, and query (with demo source code download) and zenddemo. ZendFramework tutorial-how to connect to the database and perform addition, deletion, and query (with demo source code download ), zenddemo this article describes how to connect to a database using the ZendFramework tutorial and how to add, delete, and query the database using the Zend Framework tutorial (see the demo source code download) and zenddemo
This example describes how to connect to a database and perform addition, deletion, and query in the Zend Framework tutorial. We will share this with you for your reference. The details are as follows:
First, create a table named message in the database. It has three fields: id, title, and content. id is the primary key.
Now let's start the first step: add a config folder under the application folder, and add a config. ini file here. this is the basic information of the configuration database.
The following code is used:
[General] db. adapter = PDO_MYSQL // enable PDO extension db. config. host = localhost // Mysql host db. config. username = root // username db. config. password = // password, which is empty here. config. dbname = zendoophp // database name
Step 2: add a Message. php file under the models folder under application. the name here is the same as the data table name.
<?phpclass Message extends Zend_Db_Table {protected $_name ="message";protected $_primary = 'id';}
Step 3: Add the following code to index. php in our entry file:
// Configure the database parameters and connect to the database $ config = new Zend_Config_Ini ('. /application/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: setdefaadapter adapter ($ dbAdapter); Zend_Registry: SET ('dbadapter ', $ dbAdapter );
Step 4: We need to operate our IndexController. php controller. There are four methods respectively. Their role is to add data, modify,
Delete data. The program is as follows .. (I have annotations for Programmers. I will not talk about it 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 (); // instantiate the database class // assign a value to the variable in the index. the phtml template shows $ this-> view-> bodyTitle = 'Hello World! '; // Obtain all data. 2d array $ this-> view-> messages = $ message-> fetchAll ()-> toArray (); // print_r ($ this-> view-> messages ); echo $ this-> view-> render ('index. phtml'); // Display template} function addAction () {// if it is a POST value. add. otherwise, add the page if (strtolower ($ _ SERVER ['request _ method']) = 'post') {// filter some data. however, some actions are not detected here .. // Please add .. I will not write that much. time relationship .. zend_Loader: loadClass ('zend _ filter_striptags'); $ filter = new Zend_Filter_StripTags (); $ con Tent = $ 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 have added data! Please $ this-> view-> baseUrl. '/index/"> Return';} else {echo $ this-> view-> render ('add. phtml '); // add template to display} 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); // update table data $ message-> update ($ set, $ where) unset ($ set); echo' you have modified the data! Please $ this-> view-> baseUrl. '/index/"> Return ';} else {$ id = $ filter-> filter ($ this-> _ request-> getParam ('id '))); $ this-> view-> messages = $ message-> fetchAll ('Id = '. $ id)-> toArray (); echo $ this-> view-> render ('edit. phtml'); // display the editing Template} public function delAction () {$ message = new Message (); // delete data by ID. some actions are not performed here. for example, there is no ID page to go. //. I just want to give you an idea .. so it is not so complete $ id = (int) $ this-> _ request-> getParam ('id'); if ($ id> 0) {$ wh Ere = 'Id = '. $ id; $ message-> delete ($ where);} echo' you have deleted the data! Please $ this-> view-> baseUrl. '/index/"> Return ';}}
Step 5: add the corresponding View. that is, the webpage template .. are add. phtml, edit. phtml, index. phtml. this is also annotated in the program. please download the file and run it.
Click here to download the complete instance code.