Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP数据库操作基类(单例模式)

PHP数据库操作基类(单例模式)

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


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:消耗内存用的 Artikel seterusnya:复制移动文件 - PHP