


Zend Framework tutorial: How to connect to the database and perform addition and deletion queries (with demo source code download), zenddemo_PHP tutorial
Jul 12, 2016 am 08:56 AMZend 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 //請(qǐng)開啟PDO擴(kuò)展 db.config.host=localhost //Mysql主機(jī) db.config.username=root //用戶名 db.config.password= //密碼,我這里為空 db.config.dbname=zendoophp //數(shù)據(jù)庫(kù)名
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.
<?php class Message extends Zend_Db_Table { protected $_name ="message"; protected $_primary = 'id'; }
Step 3: Next.. We need to add the following code to our entry file index.php as follows:
//配置數(shù)據(jù)庫(kù)參數(shù),并連接數(shù)據(jù)庫(kù) $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 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();//實(shí)例化數(shù)據(jù)庫(kù)類 //這里給變量賦值,在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過來的值.就增加.否則就顯示增加頁(yè)面 if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ //過濾一些數(shù)據(jù).不過這里還有檢測(cè)一些動(dòng)作沒有做.. //請(qǐng)大家加了..我就不多寫那么多了.時(shí)間關(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ù)成功!請(qǐng)您 $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ù)成功!請(qǐng)您 $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ù).這里有一些動(dòng)作沒有做.比如說沒有ID頁(yè)面要去哪里. //.我只是給大家一個(gè)思想..所以不會(huì)那么完整 $id = (int)$this->_request->getParam('id'); if ($id > 0) { $where = 'id = ' . $id; $message->delete($where); } echo '您刪除數(shù)據(jù)成功!請(qǐng)您 $this->view->baseUrl.'/index/index/">返回'; } }
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

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)

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.
