Blogger Information
Blog 16
fans 0
comment 0
visits 11214
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库查询构造器的增删查改
逃逃
Original
507 people have browsed it

数据库查询构造器的增删查改

[toc] //md 内容表

  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. //抽象类
  13. abstract class aDb implements iDbBase
  14. {
  15. // 创建类的唯一实例 pdo对象
  16. private static $_instance;
  17. // private私有的 阻止此类在外部进行实例化
  18. private function __construct()
  19. {
  20. }
  21. // private私有的 阻止此类在外部进行克隆
  22. private function __clone(){}
  23. static function doConnect($dsn,$username,$password) //静态方法
  24. {
  25. // 得到pdo连接对象 储存在 $_instance
  26. if(is_null(self::$_instance))
  27. {
  28. self::$_instance = new PDO($dsn,$username,$password); //类的引用 访问静态成员
  29. }
  30. return self::$_instance;
  31. }
  32. }
  33. // 工作类
  34. class Db extends aDb{
  35. //数据库操作 curd
  36. static function insert($db,$data){
  37. }
  38. //数据库查询构造器
  39. static function select($db,$where=['position'=>1,'id'=>1]){
  40. // select filed.. form tableName where gender=1 and id>1
  41. // select filed.. form tableName where gender=1
  42. $str = '';
  43. foreach($where as $k=>$v)
  44. {
  45. if(is_array($where))
  46. {
  47. if(count($where) > 1)
  48. {
  49. $str .= $k . ' = ' . $v . ' and ';
  50. }else{
  51. $str .= $k . '=' . $v;
  52. }
  53. }
  54. }
  55. if(count($where) > 1)
  56. {
  57. // 去掉where中的最后一个and
  58. $str = substr($str,0,strlen($str)-4);
  59. // echo $str;
  60. }
  61. echo $str;
  62. // SELECT `uname`, `tel` FROM `iuser` WHERE gender = 1 and id = 20201
  63. return $db->query("SELECT `title`, `url` FROM `oyk_ad` WHERE $str ")->fetchAll(PDO::FETCH_ASSOC);
  64. }
  65. //数据库查询构造器的增
  66. static function delete($db,$where=['gender'=>1,'uid'=>20201]){
  67. //INSERT INTO 表名称 VALUES ('值', '值1', '值2', '值3')
  68. return $db->query("INSERT INTO `iuser`('chloe','zhoujielun521','951242@qq.com','13113113166','','0','1',) ";
  69. }
  70. //数据库查询构造器的删
  71. static function delete($db,$where=['gender'=>1,'uid'=>20201]){
  72. //DELETE FROM 表名称 WHERE 列名称 = '值'
  73. return $db->query("DELETE FROM `iuser` WHERE uid = '20201' ";
  74. }
  75. //数据库查询构造器的查
  76. static function delete($db,$where=['gender'=>1,'uid'=>20201]){
  77. //SELECT 列名称(可多个,返回数组) FROM 表名称
  78. return $db->query("SELECT `title`, `url` FROM `iuser` WHERE uid = '20201' ";
  79. }
  80. //数据库查询构造器的改
  81. static function update($db,$data,$where=['gender'=>1,'uid'=>20201]){
  82. //UPDATE 表名称 SET 列名称 = '新值' WHERE 列名称 = '某值'
  83. return $db->query("UPDATE FROM `iuser` SET uname = '大帅哥胡歌' WHERE uid = '20201' ";
  84. }
  85. }
  86. // 客户端代码
  87. $dsn = 'mysql:host=localhost;dbname=chloe';
  88. $db = Db::doConnect($dsn,'root','zhoujielun521');
  89. // var_dump($db);
  90. print_r(Db::select($db));

总结

  • static function insert($db,$data); //插入
  • static function select($db,$where=[]); //查询
  • static function delete($db,$where=[]); //删除
  • static function update($db,$data,$where=[]); //更改
  • static function doConnect($dsn,$username,$password); //连接

  • INSERT INTO 表名称 VALUES (‘值’, ‘值 1’, ‘值 2’, ‘值 3’)

  • DELETE FROM 表名称 WHERE 列名称 = ‘值’
  • SELECT 列名称(可多个,返回数组) FROM 表名称
  • UPDATE 表名称 SET 列名称 = ‘新值’ WHERE 列名称 = ‘某值’
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post