< ? 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) ; //输出测试结果
? >
好长时间没用的------php-----数据库连接类
最新推荐文章于 2024-04-24 15:44:04 发布