1. <?php
  2. // 单例模式连接数据库
  3. interface iDbBase
  4. {
  5. //数据库操作 curd
  6. static function insert($db,$data);
  7. static function select($db,$where=[]);
  8. static function delete($db,$where=[]);
  9. static function update($db,$data,$where=[]);
  10. static function doConnect($dsn,$username,$password);
  11. }
  12. abstract class aDb implements iDbBase
  13. {
  14. // 创建类的唯一实例 pdo对象
  15. private static $_instance;
  16. // private私有的 阻止此类在外部进行实例化
  17. private function __construct()
  18. {
  19. }
  20. // private私有的 阻止此类在外部进行克隆
  21. private function __clone()
  22. {
  23. }
  24. static function doConnect($dsn,$username,$password)
  25. {
  26. // 得到pdo连接对象 储存在 $_instance
  27. if(is_null(self::$_instance))
  28. {
  29. self::$_instance = new PDO($dsn,$username,$password);
  30. }
  31. return self::$_instance;
  32. }
  33. }
  34. // 工作类
  35. class Db extends aDb{
  36. //数据库操作 curd
  37. static function insert($db,$data=''){
  38. return $db->query("INSERT INTO t_user ( name, type)VALUES('张杰','1')")->fetchAll(PDO::FETCH_ASSOC);
  39. }
  40. static function select($db,$where=['type'=>1]){
  41. // select filed.. form tableName where gender=1 and id>1
  42. // select filed.. form tableName where gender=1
  43. $str = '';
  44. foreach($where as $k=>$v)
  45. {
  46. if(is_array($where))
  47. {
  48. if(count($where) > 1)
  49. {
  50. $str .= $k . ' = ' . $v . ' and ';
  51. }else{
  52. $str .= $k . '=' . $v;
  53. }
  54. }
  55. }
  56. if(count($where) > 1)
  57. {
  58. // 去掉where中的最后一个and
  59. $str = substr($str,0,strlen($str)-4);
  60. // echo $str;
  61. }
  62. return $db->query("SELECT * FROM `t_user` WHERE $str ")->fetchAll(PDO::FETCH_ASSOC);
  63. }
  64. static function delete($db,$where=['id'=>4]){
  65. $str = '';
  66. foreach($where as $k=>$v)
  67. {
  68. if(is_array($where))
  69. {
  70. if(count($where) > 1)
  71. {
  72. $str .= $k . ' = ' . $v . ' and ';
  73. }else{
  74. $str .= $k . '=' . $v;
  75. }
  76. }
  77. }
  78. if(count($where) > 1)
  79. {
  80. // 去掉where中的最后一个and
  81. $str = substr($str,0,strlen($str)-4);
  82. // echo $str;
  83. }
  84. return $db->query("DELETE FROM t_user WHERE $str")->fetchAll(PDO::FETCH_ASSOC);
  85. }
  86. static function update($db,$data=2,$where=['name'=>'周杰伦','type'=>2]){
  87. $str = '';
  88. foreach($where as $k=>$v)
  89. {
  90. if(is_array($where))
  91. {
  92. if(count($where) > 1)
  93. {
  94. $str .= $k . ' = ' . '\'' . $v . '\''. ' , ';
  95. }else{
  96. $str .= $k . '=' . $v;
  97. }
  98. }
  99. }
  100. if(count($where) > 1)
  101. {
  102. // 去掉where中的最后一个and
  103. $str = substr($str,0,strlen($str)-2);
  104. echo $str;
  105. }
  106. return $db->query("UPDATE t_user SET $str WHERE id = '$data'")->fetchAll(PDO::FETCH_ASSOC);
  107. }
  108. }
  109. // 客户端代码
  110. $dsn = 'mysql:host=localhost;dbname=test_db';
  111. $db = Db::doConnect($dsn,'test_db','123456');
  112. //var_dump($db);
  113. //print_r(Db::select($db));
  114. //print_r(Db::insert($db));
  115. //print_r(Db::delete($db));
  116. print_r(Db::update($db));

更多相关文章

  1. 为知笔记docker私有化部署
  2. 在 Delphi 中使用微软全文翻译的小例子
  3. JavaScript中是如何定义私有变量的
  4. php如何使用date去掉时分秒
  5. 为什么HashMap要自己实现writeObject和readObject方法?
  6. 虚拟私有云(Virtual Private Cloud,VPC)
  7. 云计算、公有云、私有云、混合云等
  8. 面向对象、类和对象、封装---------私有private、this关键字
  9. Nextcloud私有云盘在Centos7下的部署笔记

随机推荐

  1. Rexsee API介绍:Android屏幕锁定Keyguard
  2. Android获取手机信号强度/信号格数
  3. Android NDK入门之Hello Jni
  4. 基于MQTT实现Android消息推送(Push…
  5. Android 默认壁纸 简单分析
  6. Kotlin 风险高、RxJava 已过时,Android(安
  7. Android 如何从屏幕底部向上滑出一个view
  8. Android Android Studio 快捷键整理分享,
  9. android onTouchEvent和setOnTouchListen
  10. Android_SDK_NDK_JNI