php专区

 首页 > php专区 > PHP应用 > php类库 > PHP封装数据库操作类 - php类库

PHP封装数据库操作类 - 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,代码如下:

  1.      /** 
  2.       * 数据库配置信息 
  3.       */ 
  4.      define('DB_HOST','localhost');            //服务器 
  5.      define('DB_USER','root');                 //数据库用户名 
  6.      define('DB_PASSWORD','');                 //数据库密码 
  7.      define('DB_NAME','test0');                //默认数据库 
  8.      define('DB_CHARSET','utf8');              //数据库字符集 
  9.      define('TIMEZONE',"PRC");                 //时区设置 
  10. ?> 

接下来就是数据库操作类db.class.php,代码如下:

  1.     require_once("./configuration.php");   //引入配置常量文件 
  2.     date_default_timezone_set(TIMEZONE);   
  3.  
  4.  /** 
  5.   * 类名:DB 
  6.   * 说明:数据库操作类 
  7.   */ 
  8.  class DB 
  9.  { 
  10.   public $host;            //服务器 
  11.   public $username;        //数据库用户名 
  12.   public $password;        //数据密码 
  13.   public $dbname;          //数据库名 
  14.   public $conn;            //数据库连接变量 
  15.    
  16.   /** 
  17.    * DB类构造函数 
  18.    */ 
  19.   public function DB($host=DB_HOST ,$username=DB_USER,$password=DB_PASSWORD,$dbname=DB_NAME) 
  20.   { 
  21.    $this->host = $host
  22.    $this->username = $username
  23.    $this->password = $password
  24.    $this->dbname = $dbname
  25.     
  26.   } 
  27.   /** 
  28.    * 打开数据库连接 
  29.    */ 
  30.   public function open() 
  31.   { 
  32.    $this->conn = mysql_connect($this->host,$this->username,$this->password); 
  33.    mysql_select_db($this->dbname); 
  34.    mysql_query("SET CHARACTER SET utf8"); 
  35.   } 
  36.   /** 
  37.    * 关闭数据连接 
  38.    */ 
  39.   public function close() 
  40.   { 
  41.    mysql_close($this->conn); 
  42.   } 
  43.   /** 
  44.    * 通过sql语句获取数据 
  45.    * @return: array() 
  46.    */ 
  47.   public function getObjListBySql($sql
  48.   { 
  49.    $this->open(); 
  50.    $rs = mysql_query($sql,$this->conn); 
  51.    $objList = array(); 
  52.    while($obj = mysql_fetch_object($rs)) 
  53.    { 
  54.     if($obj
  55.     { 
  56.      $objList[] = $obj
  57.     } 
  58.    } 
  59.    $this->close(); 
  60.    return $objList
  61.   } 
  62.    
  63.      /** 
  64.    * 向数据库表中插入数据 
  65.    * @param:$table,表名 
  66.    * @param:$columns,包含表中所有字段名的数组。默认空数组,则是全部有序字段名 
  67.    * @param:$values,包含对应所有字段的属性值的数组 
  68.    */ 
  69.   public function insertData($table,$columns=array(),$values=array()) 
  70.   { 
  71.    $sql = 'insert into '.$table .'( '
  72.    for($i = 0; $i < sizeof($columns);$i ++) 
  73.    { 
  74.     $sql .= $columns[$i]; 
  75.     if($i < sizeof($columns) - 1) 
  76.     { 
  77.      $sql .= ','
  78.     } 
  79.    } 
  80.    $sql .= ') values ( ';  
  81.    for($i = 0; $i < sizeof($values);$i ++) 
  82.    { 
  83.     $sql .= "'".$values[$i]."'"
  84.     if($i < sizeof($values) - 1) 
  85.     { 
  86.      $sql .= ','
  87.     } 
  88.    } 
  89.    $sql .= ' )'
  90.    $this->open(); 
  91.    mysql_query($sql,$this->conn); 
  92.    $id = mysql_insert_id($this->conn); 
  93.    $this->close(); 
  94.    return $id
  95.   } 
  96.    
  97.   /** 
  98.    * 通过表中的某一属性获取数据 
  99.    */ 
  100.   public function getDataByAtr($tableName,$atrName,$atrValue){ 
  101.    @$data = $this->getObjListBySql("SELECT * FROM ".$tableName." WHERE $atrName = '$atrValue'"); 
  102.    if(count($data)!=0)return $data
  103.    return NULL; //开源代码phpfensi.com 
  104.    } 
  105.   /** 
  106.    * 通过表中的"id",删除记录 
  107.    */ 
  108.    public function delete($tableName,$atrName,$atrValue){ 
  109.     $this->open(); 
  110.     $deleteResult = false; 
  111.     if(mysql_query("DELETE FROM ".$tableName." WHERE $atrName = '$atrValue'")) $deleteResult = true; 
  112.     $this->close(); 
  113.     if($deleteResultreturn true; 
  114.     else return false; 
  115.     } 
  116.   /** 
  117.    * 更新表中的属性值 
  118.    */ 
  119.    public function updateParamById($tableName,$atrName,$atrValue,$key,$value){ 
  120.     $db = new DB(); 
  121.     $db->open(); 
  122.     if(mysql_query("UPDATE ".$tableName." SET $key = '$value' WHERE $atrName = '$atrValue' ")){  //$key不要单引号 
  123.      $db->close(); 
  124.      return true; 
  125.     } 
  126.     else
  127.      $db->close(); 
  128.      return false; 
  129.     } 
  130.    } 
  131.   /*  
  132.    * @description: 取得一个table的所有属性名 
  133.    * @param: $tbName 表名 
  134.    * @return:字符串数组 
  135.    */ 
  136.   public function fieldName($tbName){ 
  137.    $resultName=array(); 
  138.    $i=0; 
  139.    $this->open(); 
  140.    $result = mysql_query("SELECT * FROM $tbName"); 
  141.    while ($property = mysql_fetch_field($result)){ 
  142.     $resultName[$i++]=$property->name; 
  143.     } 
  144.    $this->close(); 
  145.    return $resultName
  146.       } 
  147.  } 
  148.  ?> 

接下来是测试了,我在phpmyadmin中建了一个test0数据库,里面建一张表user,然后用php写一个user类对应数据库中的user表.

user.class.php,代码如下:

  1.  
  2.      require_once("./db.class.php"); 
  3.  
  4.   class User{ 
  5.    public $name = NULL; 
  6.    public $password = NULL; 
  7.     
  8.    /** 
  9.     * 构造函数 
  10.     */ 
  11.    public function __construct($name,$password){ 
  12.     $this->name = $name
  13.     $this->password = $password
  14.     } 
  15.  
  16.    public function insert(){ 
  17.     $db = new DB(); 
  18.        $resultid = $db->insertData("user",array(),array('',$this->name,$this->password));  
  19.        return $resultid
  20.     } 
  21.    
  22.    public static function getUserById($uid){ 
  23.      $db = new DB(); 
  24.      return $db->getDataByAtr("user",'uid',$uid); 
  25.      } 
  26.  
  27.    public static function getUserByName($name){ 
  28.      $db = new DB(); 
  29.      @$data = $db->getObjListBySql("SELECT * FROM user WHERE name = '$name'"); 
  30.      if(count($data)!=0)return $data
  31.      else return null; 
  32.      } 
  33.  
  34.    public static function getAllUser(){ 
  35.      $db = new DB(); 
  36.       @$data = $db->getObjListBySql("SELECT * FROM user"); 
  37.       if(count($data)!=0) return $data
  38.       else return null; 
  39.      } 
  40.       
  41.    public static function deleteByUid($uid){ 
  42.      $admin = Admin::getAdminById($uid); 
  43.      $db = new DB(); 
  44.      if($db->delete("user","uid",$uid)) return true; 
  45.      else return false; 
  46.      } 
  47.    }   
  48.     
  49. ?> 

测试程序:test.php,代码如下:

  1.     header("Content-Type:text/html; charset=utf8"); 
  2.  
  3.  require_once("./user.class.php"); 
  4.  
  5.  $user = new User("HelloWorld","123456"); 
  6.  $user->insert(); 
  7.  
  8.  $users = User::getAllUser(); 
  9.  
  10.  foreach ($users as $u) { 
  11.   echo "
    "
    .$u->name."
    "
    .$u->password."
    "
  12.  } 
  13. ?>
分享到:
PHP数据库处理类 - php类库
PHP数据库处理类 在WEB应用程序开发数据库操作类是一个必不可少的东西了,当然大家可以直接连接数据库进行查询,但这样维护不方便,下面我整理了一个不错的PHP数据库处理类与各位分享. 最近在看后盾网的php视频,主要内容是相关制作一个博客系统,感觉还是很有收获,简单的数据库处理类,下面是其中...
php ZipArchive类创建和解压zip文件实例 ...
php ZipArchive类创建和解压zip文件实例 如果你使用的是php5.2以下的php版本是无法使用ZipArchive类的,只要php5.2及以上版本才可以方便的使用ZipArchive类来解压与压缩zip文件了,也能直接读取zip压缩包内的内容,很方便,这里主要总结下读取和解压的过程. 解压一个包到指定目录,代码如下: ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……