六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 848|回复: 1

PHP数据库操作基类(单例模式)-it论坛

[复制链接]
 楼主| 发表于 2013-12-20 12:19:16 | 显示全部楼层 |阅读模式
PHP数据库操作基类(单例模式)-it论坛
  1. // 配置文件
  2. <?php
  3. $db = array(
  4.                 'host'=>'localhost',
  5.                 'user'=>'root',
  6.                 'password'=>'',
  7.                 'database'=>'test',
  8. )
  9.        
  10. ?>
  11. //php 类
  12. <?php
  13. class db {
  14.         public $conn;
  15.         public static $sql;
  16.         public static $instance=null;
  17.         private function __construct(){
  18.                 require_once('db.config.php');
  19.                 $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);
  20.                 if(!mysql_select_db($db['database'],$this->conn)){
  21.                         echo "失败";
  22.                 };
  23.                 mysql_query('set names utf8',$this->conn);               
  24.         }
  25.         public static function getInstance(){
  26.                 if(is_null(self::$instance)){
  27.                         self::$instance = new db;
  28.                 }
  29.                 return self::$instance;
  30.         }
  31.         /**
  32.          * 查询数据库
  33.          */
  34.         public function select($table,$condition=array(),$field = array()){
  35.                 $where='';
  36.                 if(!empty($condition)){
  37.                        
  38.                         foreach($condition as $k=>$v){
  39.                                 $where.=$k."='".$v."' and ";
  40.                         }
  41.                         $where='where '.$where .'1=1';
  42.                 }
  43.                 $fieldstr = '';
  44.                 if(!empty($field)){
  45.                        
  46.                         foreach($field as $k=>$v){
  47.                                 $fieldstr.= $v.',';
  48.                         }
  49.                          $fieldstr = rtrim($fieldstr,',');
  50.                 }else{
  51.                         $fieldstr = '*';
  52.                 }
  53.                 self::$sql = "select {$fieldstr} from {$table} {$where}";
  54.                 $result=mysql_query(self::$sql,$this->conn);
  55.                 $resuleRow = array();
  56.                 $i = 0;
  57.                 while($row=mysql_fetch_assoc($result)){
  58.                         foreach($row as $k=>$v){
  59.                                 $resuleRow[$i][$k] = $v;
  60.                         }
  61.                         $i++;
  62.                 }
  63.                 return $resuleRow;
  64.         }
  65.         /**
  66.          * 添加一条记录
  67.          */
  68.          public function insert($table,$data){
  69.                  $values = '';
  70.                  $datas = '';
  71.                  foreach($data as $k=>$v){
  72.                          $values.=$k.',';
  73.                          $datas.="'$v'".',';
  74.                  }
  75.                  $values = rtrim($values,',');
  76.                  $datas   = rtrim($datas,',');
  77.                  self::$sql = "INSERT INTO  {$table} ({$values}) VALUES ({$datas})";
  78.                 if(mysql_query(self::$sql)){
  79.                         return mysql_insert_id();
  80.                 }else{
  81.                         return false;
  82.                 };
  83.          }
  84.          /**
  85.           * 修改一条记录
  86.           */
  87.         public function update($table,$data,$condition=array()){
  88.                 $where='';
  89.                 if(!empty($condition)){
  90.                        
  91.                         foreach($condition as $k=>$v){
  92.                                 $where.=$k."='".$v."' and ";
  93.                         }
  94.                         $where='where '.$where .'1=1';
  95.                 }
  96.                 $updatastr = '';
  97.                 if(!empty($data)){
  98.                         foreach($data as $k=>$v){
  99.                                 $updatastr.= $k."='".$v."',";
  100.                         }
  101.                         $updatastr = 'set '.rtrim($updatastr,',');
  102.                 }
  103.                 self::$sql = "update {$table} {$updatastr} {$where}";
  104.                 return mysql_query(self::$sql);
  105.         }
  106.         /**
  107.          * 删除记录
  108.          */
  109.          public function delete($table,$condition){
  110.                  $where='';
  111.                 if(!empty($condition)){
  112.                        
  113.                         foreach($condition as $k=>$v){
  114.                                 $where.=$k."='".$v."' and ";
  115.                         }
  116.                         $where='where '.$where .'1=1';
  117.                 }
  118.                 self::$sql = "delete from {$table} {$where}";
  119.                 return mysql_query(self::$sql);
  120.                
  121.          }
  122.        
  123.         public static function getLastSql(){
  124.                 echo self::$sql;
  125.         }
  126.        
  127.        
  128.        
  129. }

复制代码
2. [文件]                                             数据库操作基类 ~ 114B                                                下载(107)
  1. <?php
  2. $db = array(
  3.                 'host'=>'localhost',
  4.                 'user'=>'root',
  5.                 'password'=>'',
  6.                 'database'=>'test',
  7. )
  8.        
  9. ?>
复制代码
3. [文件]                                             数据库操作基类 ~ 3KB                                                下载(127)
  1. <?php
  2. class db {
  3.         public $conn;
  4.         public static $sql;
  5.         public static $instance=null;
  6.         private function __construct(){
  7.                 require_once('db.config.php');
  8.                 $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);
  9.                 if(!mysql_select_db($db['database'],$this->conn)){
  10.                         echo "失败";
  11.                 };
  12.                 mysql_query('set names utf8',$this->conn);               
  13.         }
  14.         public static function getInstance(){
  15.                 if(is_null(self::$instance)){
  16.                         self::$instance = new db;
  17.                 }
  18.                 return self::$instance;
  19.         }
  20.         /**
  21.          * 查询数据库
  22.          */
  23.         public function select($table,$condition=array(),$field = array()){
  24.                 $where='';
  25.                 if(!empty($condition)){
  26.                        
  27.                         foreach($condition as $k=>$v){
  28.                                 $where.=$k."='".$v."' and ";
  29.                         }
  30.                         $where='where '.$where .'1=1';
  31.                 }
  32.                 $fieldstr = '';
  33.                 if(!empty($field)){
  34.                        
  35.                         foreach($field as $k=>$v){
  36.                                 $fieldstr.= $v.',';
  37.                         }
  38.                          $fieldstr = rtrim($fieldstr,',');
  39.                 }else{
  40.                         $fieldstr = '*';
  41.                 }
  42.                 self::$sql = "select {$fieldstr} from {$table} {$where}";
  43.                 $result=mysql_query(self::$sql,$this->conn);
  44.                 $resuleRow = array();
  45.                 $i = 0;
  46.                 while($row=mysql_fetch_assoc($result)){
  47.                         foreach($row as $k=>$v){
  48.                                 $resuleRow[$i][$k] = $v;
  49.                         }
  50.                         $i++;
  51.                 }
  52.                 return $resuleRow;
  53.         }
  54.         /**
  55.          * 添加一条记录
  56.          */
  57.          public function insert($table,$data){
  58.                  $values = '';
  59.                  $datas = '';
  60.                  foreach($data as $k=>$v){
  61.                          $values.=$k.',';
  62.                          $datas.="'$v'".',';
  63.                  }
  64.                  $values = rtrim($values,',');
  65.                  $datas   = rtrim($datas,',');
  66.                  self::$sql = "INSERT INTO  {$table} ({$values}) VALUES ({$datas})";
  67.                 if(mysql_query(self::$sql)){
  68.                         return mysql_insert_id();
  69.                 }else{
  70.                         return false;
  71.                 };
  72.          }
  73.          /**
  74.           * 修改一条记录
  75.           */
  76.         public function update($table,$data,$condition=array()){
  77.                 $where='';
  78.                 if(!empty($condition)){
  79.                        
  80.                         foreach($condition as $k=>$v){
  81.                                 $where.=$k."='".$v."' and ";
  82.                         }
  83.                         $where='where '.$where .'1=1';
  84.                 }
  85.                 $updatastr = '';
  86.                 if(!empty($data)){
  87.                         foreach($data as $k=>$v){
  88.                                 $updatastr.= $k."='".$v."',";
  89.                         }
  90.                         $updatastr = 'set '.rtrim($updatastr,',');
  91.                 }
  92.                 self::$sql = "update {$table} {$updatastr} {$where}";
  93.                 return mysql_query(self::$sql);
  94.         }
  95.         /**
  96.          * 删除记录
  97.          */
  98.          public function delete($table,$condition){
  99.                  $where='';
  100.                 if(!empty($condition)){
  101.                        
  102.                         foreach($condition as $k=>$v){
  103.                                 $where.=$k."='".$v."' and ";
  104.                         }
  105.                         $where='where '.$where .'1=1';
  106.                 }
  107.                 self::$sql = "delete from {$table} {$where}";
  108.                 return mysql_query(self::$sql);
  109.                
  110.          }
  111.        
  112.         public static function getLastSql(){
  113.                 echo self::$sql;
  114.         }
  115.        
  116.        
  117.        
  118. }

  119. $db = db::getInstance();
  120. //$list = $db->select('demo',array('name'=>'tom','password'=>'ds'),array('name','password'));
  121. //echo $db->insert('demo',array('name'=>'最近你啦','password'=>'123'));
  122. //echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1));
  123. echo $db->delete('demo',array('id'=>'2'));
  124. db::getLastSql();
  125. echo "<pre>";
  126. ?>
复制代码
本文摘自:http://www.oschina.net/code/snippet_182375_6195?p=1




该会员没有填写今日想说内容.

升级  93.67%

0

主题

0

主题

0

主题

举人

Rank: 3Rank: 3

积分
481
发表于 2013-12-23 03:29:18 | 显示全部楼层
做做看哦,谢谢提供












国标5V3A电源适配器 CCC认证
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表