mysql和sqlite 数据库操作类

php

2009-05-18 10:27

<?

/*
2009-5-17
getlist
getrow
insert
edit
delete
query
insert_id
getnum


*/
class DB{

private $link;
public $table;
public $PK='id';
private $bug=1;

function DB($host='',$user='',$pass='',$dbname='',$char=''){
$host=empty($host)?HOST:$host;
$user=empty($user)?USER:$user;
$pass=empty($pass)?PASS:$pass;
$dbname=empty($dbname)?DBNAME:$dbname;
$char=empty($char)?CHARSET:$char;
$this->link=mysql_connect($host,$user,$pass);
!$this->link && $this->message("连接数据路错误!".mysql_errno());
$this->select_db($dbname);
$this->query("SET NAMES $char");
}


public function getlist($where="1=1",$field="*",$order="id desc",$page=0,$limit=15,$table='default_table'){
   $table=$table=="default_table"?$this->table:$table;
   $sql="select $field from ".DB_T."$table where $where order by $order limit $page,$limit ";
   $result=$this->query($sql);
   $arr=array();
   while($rs=$this->fetch($result)){
    $arr[]=$rs;
   }
return $arr;
}
//根据SQL语句得到字段值
public function getrow($sql){
    if ($this->bug)echo $sql."<p>";
   return $this->fetch($this->query($sql));
}
//根据ID取得字段值
//参数表
//参数ID
public function getid($id,$table='default_table'){
     $table=$table=="default_table"?$this->table:$table;
$sql="select * from ".DB_T."$table where {$this->PK}={$id}";
    return $this->fetch($this->query($sql));
}

public function insert($mod,$table='default_table'){
   $table=$table=="default_table"?$this->table:$table;
     !is_array($mod) && $this->mesage("参数不正确!");
   $field=array();
$value=array();
   foreach ($mod as $key => $v){
   $field[]=T_T.$key;
   $value[]=$v;
   }
   );
   );
$sql="insert into ".DB_T."$table ($fields) value ('$values')";
return $this->query($sql);
}
  
  
   //修改数据
   //表
   //数据 必须有ID
   public function edit($mod,$table='default_table'){
   $table=$table=="default_table"?$this->table:$table;
   !is_array($mod) && $this->message("参数不正确");
   !(array_key_exists($this->PK,$mod) && !empty($mod[$this->PK])) && $this->message("参数不正确");
   foreach($mod as $key=>$value){
    if ($key<>$this->PK)
    $v[]=T_T.$key."='".$value."'";
    }
  );
   $sql="update ".DB_T."{$table} set $vs where id={$mod['id']}";
   $this->query($sql);
   }
  
  
   //删除
   //参数表
   //删除的ID值
   public function delete($mod,$table='default_table'){
    $table=$table=="default_table"?$this->table:$table;
    if (is_array($mod)){
     $where="{$this->PK} in(".implode(',',$mod).")";
     }
     else{
     $where="{$this->PK}=".$mod;
     }
    $sql="delete from ".DB_T."$table where $where";
     return $this->query($sql);
   }
  
//上次影响记录数
public function getnum(){
   return mysql_affected_rows();
}
  
//最新插入ID值
    public function insert_id(){
   return @mysql_insert_id();
}

//遍历
private function fetch($result){
   return mysql_fetch_array($result);
}

//执行语句
public function query($sql,$type=1){
      if ($this->bug)echo $sql."<p>";  
$result=$type?mysql_query($sql):mysql_unbuffered_query($sql);
!$result && $this->message("SQL命令执行错误!".mysql_error());
return $result;
}

//连接数据库
private function select_db($dbname){
   ! mysql_select_db($dbname)&&$this->message("连接数据路错误!".mysql_errno());
}

//错误消息
private function message($str){
   echo die($str);
}
//关闭
public function close(){
   mysql_close($this->link);
}
//版本
public function info(){
   mysql_get_server_info($this->link);
}


}
?>

<?

/*
2009-5-18

*/
class DB{

private $link;
public $table;
public $PK='id';
private $bug=1;
private $res;

function DB($path='',$limit=300){
$path=empty($path)?DB_MAP:$path;
$this->link=sqlite_open($path);
sqlite_busy_timeout($this->link,$limit);
    !$this->link && $this->message("连接数据路错误!".sqlite_error_string(sqlite_last_error($this->link)));
}


public function getlist($where="1=1",$field="*",$order="id desc",$page=0,$limit=15,$table='default_table'){
   $table=$table=="default_table"?$this->table:$table;
     $sql="select $field from ".DB_T."$table where $where order by $order limit $page,$limit ";
   $this->res=$this->query($sql);
   $arr=array();
   while($rs=$this->fetch($this->res)){
    $arr[]=$rs;
   }
return $arr;
}
//根据SQL语句得到字段值
public function getrow($sql){
   if ($this->bug)echo $sql."<p>";
   return sqlite_array_query($this->link,$sql);
}
//根据主键取得字段值
public function getid($id,$table='default_table'){
  
  
     $table=$table=="default_table"?$this->table:$table;
$sql="select * from ".DB_T."$table where {$this->PK}={$id}";
    return $this->fetch($this->query($sql));
}

public function insert($mod,$table='default_table'){
   $table=$table=="default_table"?$this->table:$table;
     !is_array($mod) && $this->mesage("参数不正确!");
   $field=array();
$value=array();
   foreach ($mod as $key => $v){
   $field[]=T_T.$key;
   $value[]=$v;
   }
   );
   );
$sql="insert into ".DB_T."$table ($fields) values ('$values')";
return $this->query($sql);
}
  
  
   //修改数据
   //数据 必须有主键
   public function edit($mod,$table='default_table'){
   !is_array($mod) && $this->message("参数不正确");
   $table=$table=="default_table"?$this->table:$table;
     !(array_key_exists($this->PK,$mod) && !empty($mod[$this->PK])) && $this->message("参数不正确");
   foreach($mod as $key=>$value){
    if ($key<>$this->PK)
    $v[]=T_T.$key."='".$value."'";
    }
  );
   $sql="update ".DB_T."{$table} set $vs where id={$mod['id']}";
   $this->query($sql);
   }
  
  
   //删除
   public function delete($mod,$table='default_table'){
    $table=$table=="default_table"?$this->table:$table;
    if (is_array($mod)){
     $where="{$this->PK} in(".implode(',',$mod).")";
     }
     else{
     $where="{$this->PK}=".$mod;
     }
    $sql="delete from ".DB_T."$table where $where";
     return $this->query($sql);
   }
  
//上次影响记录数
public function getnum(){
   return @sqlite_num_rows($this->res);
}
  
//最新插入ID值
    public function insert_id(){
   return @sqlite_last_insert_rowid($this->link);
}

//遍历
private function fetch($result){
   return sqlite_fetch_array($result);
}

//执行语句
public function query($sql,$type=1){
      if ($this->bug)echo $sql."<p>";  
$this->res= $type?sqlite_query($this->link,$sql):sqlite_unbuffered_query($this->link,$sql);
!$this->res && $this->message("SQL语句执行错误!".sqlite_error_string(sqlite_last_error($this->link)));
return $this->res;
}

//相关信息
    public function info(){
return '编码:'.sqlite_libencoding().'版本:'. sqlite_libversion();
}


//错误消息
private function message($str){
   echo die($str);
}

//关闭
public function close(){
   sqlite_close($this->link);
}
}
?>