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

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 //数据库名

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';
}

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);

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/">返回'; 
 } 
}

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...
Statement
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
Microsoft NET Framework 安装问题 错误代码 0x800c0006 修复Microsoft NET Framework 安装问题 错误代码 0x800c0006 修复May 05, 2023 pm 04:01 PM

.NETFramework4是开发人员和最终用户在Windows上运行最新版本的应用程序所必需的。但是,在下载安装.NETFramework4时,许多用户抱怨安装程序在中途停止,显示以下错误消息-“ .NETFramework4hasnotbeeninstalledbecauseDownloadfailedwitherrorcode0x800c0006 ”。在您的设备上安装.NETFramework4时,如果您也在体验它,那么您就来对了地方

如何在 Windows 11/10 上使用 SetupDiag 识别 Windows 升级问题如何在 Windows 11/10 上使用 SetupDiag 识别 Windows 升级问题Apr 17, 2023 am 10:07 AM

每当您的Windows11或Windows10PC出现升级或更新问题时,您通常会看到一个错误代码,指示故障背后的实际原因。但是,有时,升级或更新失败可能不会显示错误代码,这时就会出现混淆。有了方便的错误代码,您就可以确切地知道问题出在哪里,因此您可以尝试修复。但是由于没有出现错误代码,因此识别问题并解决它变得极具挑战性。这会占用您大量时间来简单地找出错误背后的原因。在这种情况下,您可以尝试使用Microsoft提供的名为SetupDiag的专用工具,该工具可帮助您轻松识别错误背后的真

深入理解MySQL索引优化器工作原理深入理解MySQL索引优化器工作原理Nov 09, 2022 pm 02:05 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

SCNotification 已停止工作 [修复它的 5 个步骤]SCNotification 已停止工作 [修复它的 5 个步骤]May 17, 2023 pm 09:35 PM

作为Windows用户,您很可能会在每次启动计算机时遇到SCNotification已停止工作错误。SCNotification.exe是一个微软系统通知文件,由于权限错误和点网故障等原因,每次启动PC时都会崩溃。此错误也以其问题事件名称而闻名。因此,您可能不会将其视为SCNotification已停止工作,而是将其视为错误clr20r3。在本文中,我们将探讨您需要采取的所有步骤来修复SCNotification已停止工作,以免它再次困扰您。什么是SCNotification.e

Microsoft .NET Framework 4.5.2、4.6 和 4.6.1 将于 2022 年 4 月终止支持Microsoft .NET Framework 4.5.2、4.6 和 4.6.1 将于 2022 年 4 月终止支持Apr 17, 2023 pm 02:25 PM

已安装Microsoft.NET版本4.5.2、4.6或4.6.1的MicrosoftWindows用户如果希望Microsoft将来通过产品更新支持该框架,则必须安装较新版本的Microsoft框架。据微软称,这三个框架都将在2022年4月26日停止支持。支持日期结束后,产品将不会收到“安全修复或技术支持”。大多数家庭设备通过Windows更新保持最新。这些设备已经安装了较新版本的框架,例如.NETFramework4.8。未自动更新的设备可能

数据库系统的构成包括哪些数据库系统的构成包括哪些Jul 15, 2022 am 11:58 AM

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

适用于 Windows 11 的KB5012643破坏了.NET Framework 3.5应用程序适用于 Windows 11 的KB5012643破坏了.NET Framework 3.5应用程序May 09, 2023 pm 01:07 PM

自我们谈论影响安装KB5012643forWindows11的用户的新安全模式错误以来已经过去了一周。这个讨厌的问题并没有出现在微软在发布当天发布的已知问题列表中,因此让所有人都感到意外。好吧,就在您认为情况不会变得更糟的时候,微软为安装此累积更新的用户投下了另一颗炸弹。Windows11Build22000.652导致更多问题因此,这家科技公司警告Windows11用户,他们在启动和使用某些.NETFramework3.5应用程序时可能会遇到问题。听起来很熟悉?不过请不要惊

如何在Zend框架中使用ACL(Access Control List)进行权限控制如何在Zend框架中使用ACL(Access Control List)进行权限控制Jul 29, 2023 am 09:24 AM

如何在Zend框架中使用ACL(AccessControlList)进行权限控制导言:在一个Web应用程序中,权限控制是至关重要的一项功能。它可以确保用户只能访问其有权访问的页面和功能,并防止未经授权的访问。Zend框架提供了一种方便的方法来实现权限控制,即使用ACL(AccessControlList)组件。本文将介绍如何在Zend框架中使用ACL

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.