Home > Backend Development > PHP Tutorial > Zend Framework tutorial - Zend_Db_Table_Rowset usage example analysis, zendframework2 example_PHP tutorial

Zend Framework tutorial - Zend_Db_Table_Rowset usage example analysis, zendframework2 example_PHP tutorial

WBOY
Release: 2016-07-12 08:56:05
Original
766 people have browsed it

Zend Framework tutorial - Zend_Db_Table_Rowset usage example analysis, zendframework2 example

This article describes the Zend Framework Zend_Db_Table_Rowset usage example. Share it with everyone for your reference, the details are as follows:

1. Introduction

Zend_Db_Table_Rowset is an iterator of the Zend_Db_Table_Row object collection. Generally speaking, you cannot instantiate Zend_Db_Table_Rowset yourself, but return Zend_Db_Table_Rowset as the result data by calling the Zend_Db_Table::find() method or the fetchAll() method. Next You can traverse the Zend_Db_Table_Row object collection and modify it.

2. Retrieve the result set

First, you need to instantiate a Zend_Db_Table class.

<&#63;php
// 设置一个 adapter
require_once 'Zend/Db.php';
$params = array (
  'host'   => '127.0.0.1',
  'username' => 'malory',
  'password' => '******',
  'dbname'  => 'camelot'
);
$db = Zend_Db::factory('PDO_MYSQL', $params);
// 为所有的Zend_Db_Table对象设置默认
require_once 'Zend/Db/Table.php';
Zend_Db_Table::setDefaultAdapter($db);
// 连接数据库表
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();
&#63;>
Copy after login

Next, you can use the Zend_Db_Table::find() method and multiple key values, or use the Zend_Db_Table::fetchAll() method to query the database,
The returned result is a Zend_Db_Table_Rowset object, through which each Zend_Db_Table_Row object in the result set can be traversed.

<&#63;php
// 从表中取回多条记录
$rowset = $table->fetchAll();
//
// $rowset现在是一个Zend_Db_Table_Rowset对象,该对象中每条记录就是一个Zend_Db_Table_Row对象
//
&#63;>

Copy after login

3. Traverse the result set

Zend_Db_Table_Rowset implements the iterator interface of a simple programming language, that is to say, the Zend_Db_Table_Rowset object can be processed in a loop, just like using the foreach() function to process an array. Each value retrieved using this method is A Zend_Db_Table_Row object corresponding to the data in the table. You can view, modify and save the properties of the object (i.e. the field values ​​in the table.)

<&#63;php
// 连接到数据库中的表
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();
// 从表中取回多条记录
$rowset = $table->fetchAll();
// 显示所有的记录
foreach ($rowset as $row) {
  // $row 是一个 Zend_Db_Table_Row 对象
  echo "<p>" . htmlspecialchars($row->nobleTitle) . " "
    . htmlspecialchars($row->firstName) . "'s "
    . "favorite color is " . htmlspecialchars($row->favoriteColor)
    . ".</p>/n";
  // 更新我们显示改行的次数
  // (对应表中的"times_displayed"字段)
  $row->timesDisplayed ++;
  // 保存新记录.
  $row->save();
}
&#63;>

Copy after login

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_Row usage example analysis
  • Zend Framework tutorial - detailed explanation of Zend_Db_Table usage
  • Zend Framework tutorial Zend_Form component implements form submission and displays error prompts
  • Zend Framework development 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 the method of storing sessions in memcache
  • Detailed explanation of usage of Zend Framework paging class
  • Zend Framework implements multiple file upload function example
  • Environment configuration for getting started with Zend Framework and the first Hello World example (with demo source code download)
  • Connecting the database in Zend Framework tutorial And perform the method of adding and deleting the query (with demo source code download)
  • Detailed explanation of the Zend_Db_Table table association instance in the Zend Framework tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113740.htmlTechArticleZend Framework tutorial - Zend_Db_Table_Rowset usage example analysis, zendframework2 example This article describes the Zend Framework Zend_Db_Table_Rowset usage example. Share it with everyone...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template