T086学习网 | 站长学院 | 技术文档 | 成语 | 歇后语 | 帝国时代 | 代码收藏 | IP地址查询 | 生活百科 | 生日密码 | CSS压缩 | 用户评论 | 欣欣百宝箱

Smarty程序应用范例:留言簿(Guestbook)

【 网络作者:Surran 更新时间:2006-12-05 | 字体:
[导读]翻译:Surran pkstudio_comeback@yahoo.com转载请注明出处和译者原文见:http://smarty.php.net/sampleapp/sampleapp_p1.php Smarty程序应用范例:留言簿(Guestbook)第一节 这是一个使用了Smarty的PHP应用程序。目...

Smarty程序应用范例:留言簿(Guestbook)第三节

/web/www.example.com/smarty/guestbook/libs/sql.lib.php

<?php

/**
* Project: Guestbook Sample Smarty Application
* Author: Monte Ohrt <monte [AT] ohrt [DOT] com>
* Date: March 14th, 2005
* file: sql.lib.php
* Version: 1.0
*/

// define the query types
define('SQL_NONE', 1);
define('SQL_ALL', 2);
define('SQL_INIT', 3);

// define the query formats
define('SQL_ASSOC', 1);
define('SQL_INDEX', 2);

class SQL {
    
    var $db = null;
    var $result = null;
    var $error = null;
    var $record = null;
    
    /**
     * class constructor
     */
    function SQL() { }
    
    /**
     * connect to the database
     *
     * @param string $dsn the data source name
     */
    function connect($dsn) {
        $this->db = DB::connect($dsn);

        if(DB::isError($this->db)) {
            $this->error = $this->db->getMessage();
            return false;
        }        
        return true;
    }
    
    /**
     * disconnect from the database
     */
    function disconnect() {
        $this->db->disconnect();   
    }
    
    /**
     * query the database
     *
     * @param string $query the SQL query
     * @param string $type the type of query
     * @param string $format the query format
     */
    function query($query, $type = SQL_NONE, $format = SQL_INDEX) {

        $this->record = array();
        $_data = array();
        
        // determine fetch mode (index or associative)
        $_fetchmode = ($format == SQL_ASSOC) ? DB_FETCHMODE_ASSOC
: null; $this->result = $this->db->query($query); if (DB::isError($this->result)) { $this->error = $this->result->getMessage(); return false; } switch ($type) { case SQL_ALL: // get all the records while($_row = $this->result->fetchRow($_fetchmode)) { $_data[] = $_row; } $this->result->free(); $this->record = $_data; break; case SQL_INIT: // get the first record $this->record = $this->result->fetchRow($_fetchmode); break; case SQL_NONE: default: // records will be looped over with next() break; } return true; } /** * connect to the database * * @param string $format the query format */ function next($format = SQL_INDEX) { // fetch mode (index or associative) $_fetchmode = ($format == SQL_ASSOC) ? DB_FETCHMODE_ASSOC
: null; if ($this->record = $this->result->fetchRow($_fetchmode)) { return true; } else { $this->result->free(); return false; } } } ?>



sql.lib.php 是我们基于PEAR::DB的数据库操作类的集合。它有助于尽可能地简化程序的数据库操作语法和代码。你可以拷贝以上代码,而不用过分担心是不是能理解它们,除非你觉得一定要。

接下来是相关参数的速成示例(crash course):

$guestbook->sql->query("select * from GUESTBOOK", SQL_ALL);
print_r($guestbook->sql->record);

输出结果:
Array
(
[0] => Array
(
[0] => 1
[1] => Monte
[2] => 2005-03-12 17:23:32
[3] => test entry 1
)

[1] => Array
(
[0] => 2
[1] => Monte
[2] => 2005-03-12 17:23:33
[3] => test entry 2
)

[2] => Array
(
[0] => 3
[1] => Monte
[2] => 2005-03-12 17:23:35
[3] => test entry 3
)

)

整个留言簿的内容都显示出来了。“SQL_ALL”会得到所有的查询记录。

$guestbook->sql->query("select * from GUESTBOOK");
while($guestbook->sql->next()) {
print_r($guestbook->sql->record);
}

输出结果:
Array
(
[0] => 1
[1] => Monte
[2] => 2005-03-12 17:23:32
[3] => test entry 1
)

Array
(
[0] => 2
[1] => Monte
[2] => 2005-03-12 17:23:33
[3] => test entry 2
)

Array
(
[0] => 3
[1] => Monte
[2] => 2005-03-12 17:23:35
[3] => test entry 3
)

使用循环的方式一个一个地显示所有记录。如果没有设置query()的第二个参数,那么得到的数据库记录结果会被next()从头到尾遍历。

$guestbook->sql->query("select * from GUESTBOOK", SQL_INIT);
print_r($guestbook->sql->record);

输出结果:
Array
(
[0] => 1
[1] => Monte
[2] => 2005-03-12 17:23:32
[3] => test entry 1
)

输出结果仅仅是一条数据库记录(第一条记录)。 “SQL_INIT”只得到一条记录。

$guestbook->sql->query("select * from GUESTBOOK", SQL_INIT, SQL_ASSOC);
print_r($guestbook->sql->record);

输出结果:
Array
(
[id] => 1
[Name] => Monte
[EntryDate] => 2005-03-12 17:23:32
[Comment] => test entry 1
)

把“SQL_ASSOC”作为第三参数传递给query()会使返回的结果是一个联合数组,形如:fieldname => value。

$guestbook->sql->query("select * from GUESTBOOK");
while($guestbook->sql->next(SQL_ASSOC)) {
print_r($guestbook->sql->record);
}

输出结果:
Array
(
[id] => 1
[Name] => Monte
[EntryDate] => 2005-03-12 17:23:32
[Comment] => test entry 1
)

Array
(
[id] => 2
[Name] => Monte
[EntryDate] => 2005-03-12 17:23:33
[Comment] => test entry 2
)

Array
(
[id] => 3
[Name] => Monte
[EntryDate] => 2005-03-12 17:23:35
[Comment] => test entry 3
)

把“SQL_ASSOC”作为参数传递给next()也会使返回的结果是一个联合数组。

上一页 [1] [2] 3 [4] [5] 下一页
  • 转载请注明来源:IT学习网 网址:http://www.t086.com/ 向您的朋友推荐此文章
  • 特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系我们,我们会尽快予以更正。
更多
留言建议ASP探针PHP探针站长Enjoy的Blog
© 2017 T086学习网 - T086.com(原itlearner.com)
RunTime:19.22ms QueryTime:7