<?
class Mysql{
var $linkid;
var $db_host;
var $db_user;
var $db_pwd;
var $db_name;
var $querynum=0;
var $selectid;
var $result= null;
var $pconnect = true;
//建構函式,初始化類時需帶入資料庫參數
function Mysql($db_host,$db_user,$db_pwd,$db_name,$pconnect = true){
$this->db_host =$db_host;
$this->db_user =$db_user;
$this->db_pwd =$db_pwd;
$this->db_name =$db_name;
if($this->pconnect == $pconnect){
$this->linkid=@mysql_pconnect($this->db_host,$this->db_user,$this->db_pwd);
}
else{
$this->linkid=@mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
}
if($this->linkid){
if($this->db_name!=""){
$dbselect=@mysql_select_db($this->db_name) or $this->mysql_err("database not exists");
}
}
else{
$this->Mysql_err("cannot connect the database server,pls chk your password");
}
//$this->query("SET NAMES 'utf8'");
}
//用於執行SQL語句
function Query($sql){
$this->result=@mysql_query($sql,$this->linkid) or $this->mysql_err("SQL語句".$sql."錯誤");
$this->querynum++;
return $this->result;
}
function fetch_array($sql) {
return mysql_fetch_array($sql,MYSQL_ASSOC);
}
//用於統計記錄的數目
function Number($sql){
$this->result=$this->query($sql);
$number=mysql_num_rows($this->result);
$this->Free();
return $number;
}
//取得一條記錄
function GetRow($sql){
$this->query($sql);
$row=mysql_fetch_array($this->result,MYSQL_ASSOC);
$this->Free();
return $row;
}
//取得全部記錄
function GetRows($sql){
$this->result=$this->query($sql);
while($row=mysql_fetch_array($this->result,MYSQL_ASSOC))
{
$allrows[]=$row;
}
$this->Free();
return $allrows;
}
//取得當前插入ID即ID欄位
function InsertID() {
$id = mysql_insert_id();
return $id;
}
//關閉資料庫
function Close(){
@mysql_close($this->linkid);
}
//列印錯誤資訊
function Mysql_err($msg){
echo $msg;
exit;
}
//釋放
function Free(){
@mysql_free_result($this->result);
$this->result=null;
}
}
?>