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

來源:互聯網
上載者:User
自己練習寫的資料庫操作基類,包含最基本的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. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.