PHP基于单例模式实现的数据库操作基类

这篇文章主要介绍了PHP基于单例模式实现的数据库操作基类,涉及PHP操作数据库的基本配置与增删改查等操作技巧,需要的朋友可以参考下。

本文实例讲述了PHP基于单例模式实现的数据库操作基类,分享给大家供大家参考,具体如下:

配置文件:

  1. <?php
  2. $db = array(
  3. 'host'=>'localhost',
  4. 'user'=>'root',
  5. 'password'=>'',
  6. 'database'=>'test',
  7. )
  8. ?>

php 数据库基类:

  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(!emptyempty($condition)){
  26. foreach($condition as $k=>$v){
  27. $where.=$k."='".$v."' and ";
  28. }
  29. $where='where '.$where .'1=1';
  30. }
  31. $fieldstr = '';
  32. if(!emptyempty($field)){
  33. foreach($field as $k=>$v){
  34. $fieldstr.= $v.',';
  35. }
  36. $fieldstr = rtrim($fieldstr,',');
  37. }else{
  38. $fieldstr = '*';
  39. }
  40. self::$sql = "select {$fieldstr} from {$table} {$where}";
  41. $result=mysql_query(self::$sql,$this->conn);
  42. $resuleRow = array();
  43. $i = 0;
  44. while($row=mysql_fetch_assoc($result)){
  45. foreach($row as $k=>$v){
  46. $resuleRow[$i][$k] = $v;
  47. }
  48. $i++;
  49. }
  50. return $resuleRow;
  51. }
  52. /**
  53. * 添加一条记录
  54. */
  55. public function insert($table,$data){
  56. $values = '';
  57. $datas = '';
  58. foreach($data as $k=>$v){
  59. $values.=$k.',';
  60. $datas.="'$v'".',';
  61. }
  62. $values = rtrim($values,',');
  63. $datas = rtrim($datas,',');
  64. self::$sql = "INSERT INTO {$table} ({$values}) VALUES ({$datas})";
  65. if(mysql_query(self::$sql)){
  66. return mysql_insert_id();
  67. }else{
  68. return false;
  69. };
  70. }
  71. /**
  72. * 修改一条记录
  73. */
  74. public function update($table,$data,$condition=array()){
  75. $where='';
  76. if(!emptyempty($condition)){
  77. foreach($condition as $k=>$v){
  78. $where.=$k."='".$v."' and ";
  79. }
  80. $where='where '.$where .'1=1';
  81. }
  82. $updatastr = '';
  83. if(!emptyempty($data)){
  84. foreach($data as $k=>$v){
  85. $updatastr.= $k."='".$v."',";
  86. }
  87. $updatastr = 'set '.rtrim($updatastr,',');
  88. }
  89. self::$sql = "update {$table} {$updatastr} {$where}";
  90. return mysql_query(self::$sql);
  91. }
  92. /**
  93. * 删除记录
  94. */
  95. public function delete($table,$condition){
  96. $where='';
  97. if(!emptyempty($condition)){
  98. foreach($condition as $k=>$v){
  99. $where.=$k."='".$v."' and ";
  100. }
  101. $where='where '.$where .'1=1';
  102. }
  103. self::$sql = "delete from {$table} {$where}";
  104. return mysql_query(self::$sql);
  105. }
  106. public static function getLastSql(){
  107. echo self::$sql;
  108. }
  109. }
  110. $db = db::getInstance();
  111. //$list = $db->select('demo',array('name'=>'tom','password'=>'ds'),array('name','password'));
  112. //echo $db->insert('demo',array('name'=>'脚本之家','password'=>'123'));
  113. //echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1));
  114. echo $db->delete('demo',array('id'=>'2'));
  115. db::getLastSql();
  116. echo "<pre>";
  117. ?>