IT学习者 | 文章大全 | 技术文档 | 桌面壁纸 | 实用查询 | 网络电台 | 成语 | 歇后语 | 网址 | 下载 | 周公解梦 | 生日密码 | 电视剧365 | Flash
 您现在的位置: IT学习者 >> 文章大全 >> 网络编程 >> PHP

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

【 作者:Surran    来源:网络  更新时间:2006-12-5 | 字体:

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()也会使返回的结果是一个联合数组。

文章共5页,当前在第3页 9 7 [1] [2] [3] [4] [5] 8 :
相 关 文 章
相 关 软 件
没有相关下载
逃生 放生 黄玫瑰 想太多 那滋味 擦肩而过 放手去爱 北京欢迎你 依然在一起 吻得太逼真 感动天感动地 坐上火车去拉萨 怎么会狠心伤害我
心碎 冲动 小太阳 别碰我 蒲公英 千山万水 改变自己 一定要爱你 等爱的玫瑰 陷入爱里面 北极星的眼泪 最后一次的温柔 亲爱的那不是爱情
光荣 火花 坏女人 日不落 樱花草 为你写诗 独家记忆 夏天的味道 寂寞才说爱 忘不掉的伤 爱上你是个错 第三者的第三者 地球人都知道我爱你
假如 相思 是非题 有缘人 舍不得 我的答铃 死而无憾 外滩十八号 越爱越难过 123木头人 和寂寞说分手 爱上你是我的错 爱情里没有谁对谁错
加入收藏留言建议自助友情链接普通友情链接站长的Blog
版权所有   COPYRIGHT 2002-2008 ★IT学习者★ ALL RIGHTS RESERVED.