Smarty程序應用范例:留言簿(Guestbook)第三節(jié)
/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的數(shù)據(jù)庫操作類的集合。它有助于盡可能地簡化程序的數(shù)據(jù)庫操作語法和代碼。你可以拷貝以上代碼,而不用過分擔心是不是能理解它們,除非你覺得一定要。
接下來是相關參數(shù)的速成示例(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 )
)
整個留言簿的內容都顯示出來了!癝QL_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 )
使用循環(huán)的方式一個一個地顯示所有記錄。如果沒有設置query()的第二個參數(shù),那么得到的數(shù)據(jù)庫記錄結果會被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 )
輸出結果僅僅是一條數(shù)據(jù)庫記錄(第一條記錄)。 “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”作為第三參數(shù)傳遞給query()會使返回的結果是一個聯(lián)合數(shù)組,形如: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”作為參數(shù)傳遞給next()也會使返回的結果是一個聯(lián)合數(shù)組。
出處:藍色理想
責任編輯:moby
上一頁 Smarty程序應用范例 [2] 下一頁 Smarty程序應用范例 [4]
◎進入論壇網(wǎng)絡編程版塊參加討論
|