php操作mysql类

Class MySQLDB
{
var $host;
var $user;
var $passwd;
var $database;
var $conn;
//网页执行时间,add by breezexu 2008-11-14
var $starttime;//页面开始执行时间
var $stoptime;//页面结束执行时间
var $spendtime;//页面执行花费时间
var $querynum;//执行次数
//利用构造函数实现变量初始化
//同时连接数据库操作
function MySQLDB($host,$user,$password,$database)
{
$this->host = $host;
$this->user = $user;
$this->passwd = $password;
$this->database = $database;
$this->conn=mysql_connect($this->host, $this->user,$this->passwd) or
die("Could not connect to $this->host");
mysql_select_db($this->database,$this->conn) or
die("Could not switch to database $this->database");
//mysql_query("set names 'gb2312'");
$this->starttime = $this->getmicrotime();
}
//该函数用来关闭数据库连接
function Close()
{
$this->stoptime = $this->getmicrotime();
MySQL_close($this->conn);
}
//该函数实现数据库查询操作
function Query($queryStr)
{
$res =Mysql_query($queryStr, $this->conn) or
die("Could not query database");
$this->querynum = $this->querynum+1;
return $res;
}
//该函数返回记录集
function getRows($res)
{
$rowno = 0;
$rowno = MySQL_num_rows($res);
if($rowno>0)
{
for($row=0;$row<$rowno;$row++ )
{
$rows[$row]=MySQL_fetch_array($res);
//本来为MySQL_fetch_row,但是不能以数组的方式来提取,只能用索引
//这样可以用索引和名称,更为方便
}
return $rows;
}
}
//该函数取回数据库记录数
function getRowsNum($res)
{
$rowno = 0;
$rowno = mysql_num_rows($res);
return $rowno;
}
//该函数返回数据库表字段数
function getFieldsNum($res)
{
$fieldno = 0;
$fieldno = mysql_num_fields($res);
return $fieldno;
}
//该函数返回数据库表字段名称集
function getFields($res)
{
$fno = $this->getFieldsNum($res);
if($fno>0)
{
for($i=0;$i<$fno;$i++ )
{
$fs[$i]=MySQL_field_name($res,$i);//取第i个字段的名称
}
return $fs;
}
}
//网页执行时间函数,add by breezexu
function getmicrotime()//获取返回当前微秒数的浮点数
{
$aaa =explode(" ",microtime());
return ((float)$aaa[0] + (float)$aaa[1]);
}
function start()//页面开始执行函数,返回开始页面执行的时间
{
$this->starttime=$this->getmicrotime();
}
function display()//显示页面执行的时间
{
$this->stoptime=$this->getmicrotime();
$this->spendtime=$this->stoptime-$this->starttime;
return round($this->spendtime,10);
}
function getquerynum()
{
return $this->querynum;
}
}
Tags: 

延伸阅读

最新评论

发表评论