首頁  >  文章  >  後端開發  >  PHP資料庫操作基底類別(單例模式)

PHP資料庫操作基底類別(單例模式)

WBOY
WBOY原創
2016-07-25 09:09:081004瀏覽
自己練習寫的資料庫操作基類,包含最基本的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 ."' 和 ";
  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."' 和 ";
  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 staticic $ require_once('db.config.php');
  5. $this->conn = mysql_connect($db['host'],$db['user'],$db [ ' password']);
  6. if(!mysql_select_db($db['database'],$this->conn)){
  7. echo "失敗";
  8. };
  9. mysql_query( '設定名稱utf8',$this->conn);
  10. }
  11. public static function getInstance(){
  12. if(is_null(self::$instance)){
  13. self::$instance = new db;
  14. }
  15. return self ::$instance;
  16. }
  17. /**
  18. * 查詢資料庫
  19. */
  20. public function select($table,$condition=array(),$field = array()){
  21. $where ='';
  22. if(!empty($condition)){
  23. foreach($condition as $k=>$v){
  24. $where.=$k."=' " .$v."' 和";
  25. }
  26. $where='where '.$where .'1=1';
  27. }
  28. $fieldstr = '';
  29. if ( !empty($field)){
  30. foreach($field as $k=>$v){
  31. $fieldstr.= $v.',';
  32. }
  33. $fieldstr = rtrim($fieldstr,',');
  34. }else{
  35. $fieldstr = '*';
  36. }
  37. self::$sql = "從{ 選出{$fieldstr} $table } {$where}";
  38. $result=mysql_query(self::$sql,$this->conn);
  39. $resuleRow = array();
  40. $i = 0;
  41. while ($row=mysql_fetch_assoc($result)){
  42. foreach($row as $k=>$v){
  43. $resuleRow[$i][$k] = $v;
  44. }
  45. $i++;
  46. }
  47. return $resuleRow;
  48. }
  49. /**
  50. * 新增一筆記錄
  51. */
  52. public function insert($table,$data){
  53. $values = '';
  54. $datas = '';
  55. foreach($data as $k=>$v){
  56. $values.=$k.',';
  57. $datas.=" '$v'".',';
  58. }
  59. $values = rtrim($values,',');
  60. $datas = rtrim($datas,',') ;
  61. self ::$sql = "插入{$table} ({$values}) VALUES ({$datas})";
  62. if(mysql_query(self::$sql)){
  63. return mysql_insert_id();
  64. }else{
  65. return false;
  66. };
  67. }
  68. /**
  69. * 修改一筆記錄
  70. */
  71. public function update($table,$data, $condition=array() ){
  72. $where='';
  73. if(!empty($condition)){
  74. foreach($condition as $k=>$v){
  75. $where.= $k."='".$v."' 和";
  76. }
  77. $where='where '.$where .'1=1';
  78. }
  79. $updatastr = ' ';
  80. if(!empty($data)){
  81. foreach($data as $k=>$v){
  82. $updatastr.= $k." ='".$v." ',";
  83. }
  84. $updatastr = 'set '.rtrim($updatastr,',');
  85. }
  86. self::$sql = " update {$table} {$updatastr } {$where}";
  87. return mysql_query(self::$sql);
  88. }
  89. /**
  90. * 刪除記錄
  91. */
  92. public function delete( $table,$condition){
  93. $where='';
  94. if(!empty($condition)){
  95. foreach($condition as $k=>$v){
  96. $where.=$k. "='".$v."' 和";
  97. }
  98. $where='where '.$where .'1=1';
  99. }
  100. self::$sql = " From {$table} {$where} 中刪除";
  101. return mysql_query(self::$sql);
  102. }
  103. 公用靜態函式getLastSql(){
  104. echo self::$sql;
  105. }
  106. }
  107. $db = db::getInstance();
  108. //$list = $db- >select('demo',array('name'=>'tom','password'=>'ds'),array('name','password')) ;
  109. //echo $db-> insert('demo',array('name'=>'最近你啦','password'=>'123'));
  110. //echo $db->update('demo',array("name "=>'xxx',"密碼"=>'123'),array('id'=>1));
  111. echo $db->delete('demo',array('id'=>' 2'));
  112. db::getLastSql();
  113. echo "
    ";
  114. ? >
複製程式碼

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn