ホームページ  >  記事  >  バックエンド開発  >  PHPデータベース操作基本クラス(シングルトンモード)

PHPデータベース操作基本クラス(シングルトンモード)

WBOY
WBOYオリジナル
2016-07-25 09:09:08962ブラウズ
練習用に作成したデータベース操作の基本クラスには、最も基本的な CURD 操作が含まれており、フレームワークに統合できます。
  1. // 設定ファイル
  2. $db = array(
  3. 'host'=>'localhost',
  4. 'user'=>'root',
  5. 'password'=>'' ,
  6. 'database'=>'test',
  7. )
  8. ?>
  9. //php class
  10. class db {
  11. public $conn;
  12. public static $sql;
  13. public static $instance= null;
  14. プライベート関数 __construct(){
  15. require_once('db.config.php');
  16. $this->conn = mysql_connect($db['host'],$db['user'],$db[ 'パスワード']);
  17. if(!mysql_select_db($db['データベース'],$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 = "{$ から {$fieldstr} を選択します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 = "{$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."' および ";
  108. }
  109. $where='where '.$where .'1=1';
  110. }
  111. self::$sql = "{$table} から削除{$where} ";
  112. return mysql_query(self::$sql);
  113. }
  114. public static function getLastSql(){
  115. echo self::$sql;
  116. }
  117. }
Copyコード
  1. $db = array(
  2. 'host'=>'localhost',
  3. 'user'=>'root',
  4. 'password'=>'',
  5. 'database' =>'テスト',
  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($ $k=>$v){
  26. $where.=$k."='".$v."' および ";
  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 = "{$table} {$where から {$fieldstr} を選択します}";
  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 = "{$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 ."' と ";
  99. }
  100. $where='where '.$where .'1=1';
  101. }
  102. self::$sql = "{$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('デモ',array('id'=>'2'));
  114. db::getLastSql();
  115. echo "
    ";
  116. ?>
复制代


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。