ZendFramework framework tutorial-Zend_Db_Table_Rowset usage instance analysis

Source: Internet
Author: User
This article mainly introduces the Zend_Db_Table_Rowset usage in the ZendFramework tutorial, and analyzes the related skills of Zend_Db_Table_Rowset in the form of instances, for more information about Zend Framework Zend_Db_Table_Rowset, see the following example. We will share this with you for your reference. The details are as follows:

1. Introduction

Zend_Db_Table_Rowset is the iterator of the Zend_Db_Table_Row object set. generally, you cannot instantiate Zend_Db_Table_Rowset by yourself. Instead, you can call the Zend_Db_Table: find () method or the fetchAll () method to return Zend_Db_Table_Rowset as the result data. next, we can traverse the Zend_Db_Table_Row object set and modify it.

2. retrieve the result set

First, you need to instantiate a Zend_Db_Table class.

<? Php // Set an adapterrequire_once 'zend/Db. php '; $ params = array ('host' =>' 127. 0.0.1 ', 'username' => 'malory', 'password' => '******', 'dbname' => 'Camelot '); $ db = Zend_Db: factory ('pdo _ mysql', $ params); // set the default require_once 'zend/Db/Table for all Zend_Db_Table objects. php '; Zend_Db_Table: setdefaadapter adapter ($ db); // connect to the database table class RoundTable extends Zend_Db_Table {}$ table = new RoundTable ();?>

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, which can be used to traverse each Zend_Db_Table_Row object in the result set.

<? Php // retrieve multiple records from the table $ rowset = $ table-> fetchAll (); // $ rowset is now a Zend_Db_Table_Rowset object, each record in this object is a Zend_Db_Table_Row object //?>

3. traverse the result set

Zend_Db_Table_Rowset implements the iterator interface of the simple programming language. that is to say, the Zend_Db_Table_Rowset object can be processed cyclically, just like using the foreach () function to process arrays. each value retrieved in this way is a Zend_Db_Table_Row object corresponding to the data in the table. you can view, modify, and save the attributes of this object (that is, the field value in the table .)

<? Php // connect to the database table class RoundTable extends Zend_Db_Table {}$ table = new RoundTable (); // retrieve multiple records from the table $ rowset = $ table-> fetchAll (); // display all records foreach ($ rowset as $ row) {// $ row is a Zend_Db_Table_Row object echo"

". Htmlspecialchars ($ row-> nobleTitle ). "". htmlspecialchars ($ row-> firstName ). "'s ". "favorite color is ". htmlspecialchars ($ row-> favoriteColor ). ".

/N "; // update the number of rows to be changed. // (corresponding to the" times_displayed "field in the table) $ row-> timesDisplayed ++; // Save the new record. $ row-> save () ;}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.