Zend Framework教程之连接数据库并执行增删查的方法(附demo源码下载),zenddemo
Zend Framework教程之连接数据库并执行增删查的方法(附demo源码下载),zenddemo
本文实例讲述了Zend Framework教程之连接数据库并执行增删查的方法。分享给大家供大家参考,具体如下:
我们先要在数据库里建立一个叫message的表,它有三个字段.分别为id,title,content.其中id为主键.
现在我们开始第一步:在application文件夹下面加入一个config文件夹,并在这里面增加一个config.ini文件..这里面是配置数据库基本信息.
如下代码所示:
[general] db.adapter=PDO_MYSQL //请开启PDO扩展 db.config.host=localhost //Mysql主机 db.config.username=root //用户名 db.config.password= //密码,我这里为空 db.config.dbname=zendoophp //数据库名
第二步:在application下的models文件夹下增加一个Message.php文件..这里命名是以数据表名称相同.
<?php class Message extends Zend_Db_Table { protected $_name ="message"; protected $_primary = 'id'; }
第三步:接下来..我们要在我们的入口文件index.php里加入下面代码如下:
//配置数据库参数,并连接数据库 $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);
第四步:我们就要对我们的IndexController.php控制器进行操作了..分别有四个方法.它们的作用就是增加数据,修改,
删除数据.程序如下..(我在程序员都有注解.这里不就多说!):
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 = ?', $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/">返回'; } }
第五步:就是增加对应的View.也就是网页模板..分别是add.phtml,edit.phtml,index.phtml.这在程序里也有注解.请大家下载文件运行查看.
完整实例代码点击此处本站下载。
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
- Zend Framework框架教程之Zend_Db_Table_Rowset用法实例分析
- Zend Framework教程之Zend_Db_Table_Row用法实例分析
- Zend Framework教程之Zend_Db_Table用法详解
- Zend Framework教程之Zend_Form组件实现表单提交并显示错误提示的方法
- Zend Framework开发入门经典教程
- Zend Framework框架Smarty扩展实现方法
- Zend Framework框架路由机制代码分析
- Zend Framework实现具有基本功能的留言本(附demo源码下载)
- Zend Framework实现将session存储在memcache中的方法
- Zend Framework分页类用法详解
- Zend Framework实现多文件上传功能实例
- Zend Framework入门之环境配置及第一个Hello World示例(附demo源码下载)
- Zend Framework教程之Zend_Db_Table表关联实例详解

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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)

Hot Topics



Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

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

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

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.

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.
