数据库操作类

<?php
class DB
{
 private $conn='';
 private static $param=array(
  'host'=>'localhost',
  'username'=>'root',
  'password'=>'111111',
  'ispconnect'=>false,
  'dbname'=>'zhuanti'
 );
 function __construct()
 {
  $this->connect(); 
 }
 public function connect()
 {
  try
  {
   if(self::$param['ispconnect'])
   {
    $this->conn=@mysql_pconnect(self::$param['host'],self::$param['username'],self::$param['password']);
   }
   else
   {
    $this->conn=@mysql_connect(self::$param['host'],self::$param['username'],self::$param['password']); 
   }
   if(!$this->conn)
   {
    throw new Exception("数据库链接失败"); 
   }
   else
   {
    mysql_query("set names 'gbk'"); 
   }
  }
  catch(Exception $e)
  {
   echo $e->getMessage(); 
   return false;
  }
  try
  {
   if(!@mysql_select_db(self::$param['dbname'],$this->conn))
   {
    throw new Exception("数据库不存在,请联系管理员"); 
   } 
  }
  catch(Exception $e)
  {
   echo $e->getMessage(); 
   return false;
  }
 }
 /*操作数据库*/
 public function execute($sql="")
 {
   if(empty($sql)) return false;
   if(empty($this->conn)) return false;
   try
   {
    $results=mysql_query($sql,$this->conn); 
    if(!$results)
    {
     throw new Exception("数据库操作失败");
    }
    else
    {
     return $results; 
    }
   }
   catch(Exception $e)
   {
    echo $e->getMessage();
    return false; 
   }
 }
 /*查询数据库*/
 public function getArray($sql="")
 {
  $this->sql=$sql;
  $rs=$this->execute($this->sql);
  $cont=0;
  $data=array();
  while($row=mysql_fetch_array($rs))
  {
   $data[$cont]=$row;
   $cont++; 
  }
  mysql_free_result($rs);
  return $data;
 }
 
 /*查询记录数*/
 public function getNum($sql="")
 {
  $this->sql=$sql;
  $rs=$this->execute($this->sql);
  if(!$num=mysql_num_rows($rs))
  {
   echo '数据库中没有数据'; 
  }
  else
  {
   mysql_free_result($rs);
   return $num; 
  }
 }
 /*插入数据*/
 public function getInsert($sql="")
 {
  $this->sql=$sql;
  return $this->execute($this->sql);
 } 
 /*获得插入数据的ID*/
 public function getLastId($sql="")
 {
  $this->sql=$sql;
  $rs=$this->execute($this->sql);
  return mysql_insert_id();
 }
 /*数据更新*/
 public function getUpdate($sql="")
 {
  $this->sql=$sql;
  return $this->execute($this->sql);
  
 }
 /*数据删除*/
 public function getDel($sql='')
 {
   $this->sql=$sql;
   return $this->execute($this->sql);
 }
 public function getClose()
 {
  mysql_close($this->conn); 
 }
}
?>

 

 

<?php
class DB
{
 private $conn='';
 private static $param=array(
  'host'=>'localhost',
  'username'=>'root',
  'password'=>'111111',
  'ispconnect'=>false,
  'dbname'=>'cms'
 );
 function __construct()
 {
  if(self::$param['ispconect'])
  {
   $this->conn=@mysql_pconnect(self::$param['host'],self::$param['username'],self::$param['password']); 
  }
  else
  {
   $this->conn=@mysql_connect(self::$param['host'],self::$param['username'],self::$param['password']); 
  }
   @mysql_query("set names 'gbk'"); 
 } 
 /*数据库操作*/
 public function execute($sql)
 {
  try
  {
   if(!$this->conn)
   {
    throw new Exception("数据库连接失败");
   } 
   else
   {
    try
    {
     if(!mysql_select_db(self::$param['dbname'],$this->conn))
     {
      throw new Exception("数据出选择错误,请联系管理员"); 
     } 
    }
    catch(Exception $e)
    {
     echo $e->getMessage();
     return false; 
    }
   }
  }
  catch(Exception $e)
  {
   echo $e->getMessage();
   return false; 
  }
  $results=mysql_query($sql,$this->conn);
  try
  {
   if(!$results)
   {
    throw new Exception("数据库操作失败");  
   } 
  }
  catch(Exception $e)
  {
   echo $e->getMessage();
   return false; 
  }
  return $results;
 }
 /*数据插入,修改,删除*/
 public function executeUpdate($sql="")
 {
  $this->sql=$sql;
  if(empty($this->sql)) return false;
  return $this->execute($this->sql);
 } 
 /*数据库查询*/
 public function getSelect($sql="")
 {
  $this->sql=$sql;
  if(empty($this->sql)) return false;
  $rs=$this->execute($this->sql);
  if(!$rs)
  {
   return false;
  }
  $cont=0;
  $data=array();
  while($row=mysql_fetch_array($rs))
  {
   $data[$cont]=$row;
   $cont++; 
  }
  mysql_free_result($rs);
  return $data;
 }
 /*查询数据库记录数*/
 public function getNum($sql="")
 {
  $this->sql=$sql;
  if(empty($this->sql)) return false;
  $rs=$this->execute($this->sql);
  try
  {
   $num=mysql_num_rows($rs);
   if(!$num)
   {
    throw new Exception("数据库中没有数据"); 
   } 
  }
  catch(Exception $e)
  {
   echo $e->getMessages();
   return false;
  }  
  return $num;
 }
 /*获得插入数据ID*/
 public function getId($sql="")
 {
  $this->sql=$sql;
  if(empty($this->sql)) return false;
  $this->execute($this->sql);
  return mysql_insert_id();
 }
 public function getClose()
 {
  mysql_close($this->conn);
 }
}
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值