Example analysis of zend_db_table_rowset usages of Zend framework frame Tutorial _php instance

Source: Internet
Author: User
Tags zend zend framework

The examples in this article describe the Zend_db_table_rowset usage of Zend Framework frameworks. Share to everyone for your reference, specific as follows:

1. Introduction

Zend_db_table_rowset is an iterator of a collection of Zend_db_table_row objects. Typically, you cannot instantiate the zend_db_table_rowset yourself, but by calling Zend_db_table::find () method or the Fetchall () method to return Zend_db_table_rowset as the result data. You can then iterate through the Zend_db_table_row object set merge for modification.

2. Retrieve the result set

First, you need to instantiate a zend_db_table class.

<?php
//Set up a 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);
Set the default
require_once ' zend/db/table.php ' for all zend_db_table objects;
Zend_db_table::setdefaultadapter ($db);
Connection 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 result returned is a Zend_db_table_rowset object through which each Zend_db_table_row object in the result set can be traversed.

<?php
//Retrieve multiple records from the table
$rowset = $table->fetchall ();
//$rowset is now a Zend_db_table_rowset object in which each record is a Zend_db_table_row object
//
?>

3. Traverse result set

Zend_db_table_rowset implements the iterator interface for a simple programming language, that is, the Zend_db_table_rowset object can be recycled, like using a foreach () function to work with arrays. Each value retrieved using this method is a Zend_db_table_row object of the data in the corresponding table, and you can view, modify, and save the object's properties (that is, the field values in the table).

<?php
//Connect to table class in database
Roundtable extends zend_db_table {}
$table = new Roundtable ();
Retrieving multiple records from the table
$rowset = $table->fetchall ();
Show All Records
foreach ($rowset as $row) {
  //$row is a Zend_db_table_row object
  echo "<p>". Htmlspecialchars ($r Ow->nobletitle). " "
    . Htmlspecialchars ($row->firstname). "' s"
    . " Favorite color is ". Htmlspecialchars ($row->favoritecolor)
    . ". </p>/n";
  Update the number of times we display the row
  //(the "times_displayed" field in the corresponding table)
  $row->timesdisplayed + +;
  Save the new record.
  $row->save ();
>

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

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.