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()也会使返回的结果是一个联合数组。
出处:蓝色理想
责任编辑:moby
上一页 Smarty程序应用范例 [2] 下一页 Smarty程序应用范例 [4]
◎进入论坛网络编程版块参加讨论
|