< ? php
/*
檔案名稱:DataBase.php
作用:資料庫類
作者:Mr.Bool
建立時間:2007.10.22
修改時間:2007.10.23
*/
require_once ( "Config.inc.php" ) ; //系統設定檔
/*
$DBHOST="localhost"; //主機名稱
$DBUSER="root"; // 資料庫使用者名稱
$DBPWD=""; //密碼
$DBNAME="test" ; //資料庫名
*/
//定義資料庫類
class DataBase
{
//定義屬性
var $ mConnId ; //串連標識
var $ mSqlString ; //待執行的SQL語句
var $ mResultArray ; //執行Select語句返回的結果數組
//__construct(),建構函式,建立資料庫的串連
function __construct( $ pHost , $ pUser , $ pPwd , $ pDbName ) {
$ this - > mConnId= mysql_connect ( $ pHost , $ pUser , $ pPwd ) ; //建立串連
mysql_select_db ( $ pDbName , $ this - > mConnId) ; //選擇資料庫
mysql_query ( "set names 'gbk'" ) ; //設定資料庫編碼為GBK
}
//__destruct:解構函式,中斷連線
function __destruct( ) {
mysql_close ( $ this - > mConnId) ; //此處還有問題......
}
//執行SQL語句
function ExecuteSql( ) {
mysql_query ( $ this - > mSqlString) ;
}
//查詢資料,傳回值為對象數組,數組中的每一元素為一行記錄構成的對象
function Query( ) {
$ i = 0;
$ query_result = mysql_query ( $ this - > mSqlString, $ this - > mConnId) ;
while ( $ row = mysql_fetch_object ( $ query_result ) ) {
$ this - > mResultArray[ $ i + + ] = $ row ;
}
}
} //class DataBase
//以下為測試用
$ db = new DataBase( $ DBHOST , $ DBUSER , $ DBPWD , $ DBNAME ) ;
$ db - > mSqlString= "update student set phone='123' where id='04261001' " ;
$ db - > ExecuteSql( ) ;
$ db - > mSqlString= "select * from student where id='04261001' " ;
$ db - > Query( ) ;
print_r ( $ db - > mResultArray) ; //輸出測試結果
? >