您好,欢迎访问一九零五行业门户网

PHP封装数据库操作类(链接数据库)_PHP教程

我们在网站开发时比较合理的做法就是我们的常用的一些程序做成函数或封闭成类,这样可以重复利用,可以节约开发成本了,下面我来给各位介绍了我常使用的类。
有面向对象技术基础的编程人员看一天就可以写起来了。而php在访问数据库的时候又经常会出现各种问题,如字符编码问题、sql语法错误问题、php处理数据记录对象和返回对象的问题等。我这里写了一个数据库操作类,封装了数据库增删添改等操作,很方便使用。用这个类,可以加速网站的后台开发。
优点:
1.方便快捷, 数据库操作只需调用接口;
2.统一编码(utf8),不易导致乱码
3.结构清晰. 如处理前端请求的后台程序(test.php) + 表封装类(user.class.php) + 数据库封装类(db.class.php) + 配置信息(configuration.php)
以下例子有四个文件: configuration.php + db.class.php + user.class.php + test.php,放在同一个目录下。
首先是一个数据库配置的文件类configuration.php
 代码如下 复制代码
接下来就是数据库操作类db.class.php
 代码如下 复制代码
host = $host;
   $this->username = $username;
   $this->password = $password;
   $this->dbname = $dbname;
}
  /**
   * 打开数据库连接
   */
  public function open()
  {
   $this->conn = mysql_connect($this->host,$this->username,$this->password);
   mysql_select_db($this->dbname);
   mysql_query(set character set utf8);
  }
  /**
   * 关闭数据连接
   */
  public function close()
  {
   mysql_close($this->conn);
  }
  /**
   * 通过sql语句获取数据
   * @return: array()
   */
  public function getobjlistbysql($sql)
  {
   $this->open();
   $rs = mysql_query($sql,$this->conn);
   $objlist = array();
   while($obj = mysql_fetch_object($rs))
   {
    if($obj)
    {
     $objlist[] = $obj;
    }
   }
   $this->close();
   return $objlist;
  }
/**
   * 向数据库表中插入数据
   * @param:$table,表名
   * @param:$columns,包含表中所有字段名的数组。默认空数组,则是全部有序字段名
   * @param:$values,包含对应所有字段的属性值的数组
   */
  public function insertdata($table,$columns=array(),$values=array())
  {
   $sql = 'insert into '.$table .'( ';
   for($i = 0; $i    {
    $sql .= $columns[$i];
    if($i     {
     $sql .= ',';
    }
   }
   $sql .= ') values ( ';
   for($i = 0; $i    {
    $sql .= '.$values[$i].';
    if($i     {
     $sql .= ',';
    }
   }
   $sql .= ' )';
   $this->open();
   mysql_query($sql,$this->conn);
   $id = mysql_insert_id($this->conn);
   $this->close();
   return $id;
  }
/**
   * 通过表中的某一属性获取数据
   */
  public function getdatabyatr($tablename,$atrname,$atrvalue){
   @$data = $this->getobjlistbysql(select * from .$tablename. where $atrname = '$atrvalue');
   if(count($data)!=0)return $data;
   return null; 
   }
  /**
   * 通过表中的id,删除记录
   */
   public function delete($tablename,$atrname,$atrvalue){
    $this->open();
    $deleteresult = false;
    if(mysql_query(delete from .$tablename. where $atrname = '$atrvalue')) $deleteresult = true;
    $this->close();
    if($deleteresult) return true;
    else return false;
    }
  /**
   * 更新表中的属性值
   */
   public function updateparambyid($tablename,$atrname,$atrvalue,$key,$value){
    $db = new db();
    $db->open();
    if(mysql_query(update .$tablename. set $key = '$value' where $atrname = '$atrvalue' )){  //$key不要单引号
     $db->close();
     return true;
    }
    else{
     $db->close();
     return false;
    }
   }
  /*
   * @description: 取得一个table的所有属性名
   * @param: $tbname 表名
   * @return:字符串数组
   */
  public function fieldname($tbname){
   $resultname=array();
   $i=0;
   $this->open();
   $result = mysql_query(select * from $tbname);
   while ($property = mysql_fetch_field($result)){
    $resultname[$i++]=$property->name;
    }
   $this->close();
   return $resultname;
      }
 }
 ?>
接下来是测试了。我在phpmyadmin中建了一个test0数据库,里面建一张表user。然后用php写一个user类对应数据库中的user表。
user.class.php
 代码如下 复制代码
name = $name;
    $this->password = $password;
    }
   public function insert(){
    $db = new db();
       $resultid = $db->insertdata(user,array(),array('',$this->name,$this->password)); 
       return $resultid;
    }
public static function getuserbyid($uid){
     $db = new db();
     return $db->getdatabyatr(user,'uid',$uid);
     }
public static function getuserbyname($name){
     $db = new db();
     @$data = $db->getobjlistbysql(select * from user where name = '$name');
     if(count($data)!=0)return $data;
     else return null;
     }
   public static function getalluser(){
     $db = new db();
      @$data = $db->getobjlistbysql(select * from user);
      if(count($data)!=0) return $data;
      else return null;
     }
public static function deletebyuid($uid){
     $admin = admin::getadminbyid($uid);
     $db = new db();
     if($db->delete(user,uid,$uid)) return true;
     else return false;
     }
   }
?>
测试程序: test.php
 代码如下 复制代码
insert();
 $users = user::getalluser();
 foreach ($users as $u) {
  echo
.$u->name.
.$u->password.
;
 }
?>
http://www.bkjia.com/phpjc/630692.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/630692.htmltecharticle我们在网站开发时比较合理的做法就是我们的常用的一些程序做成函数或封闭成类,这样可以重复利用,可以节约开发成本了,下面我来给...
其它类似信息

推荐信息