实例数据库连接类
<?php 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); //输出测试结果
教程网址:http://www.phprm.com/code/06f6f5dc531247594e888afe21ddc6c0.html
欢迎收藏∩_∩但请保留本文链接。