Blogger Information
Blog 28
fans 0
comment 0
visits 16915
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月27日 - 数据库操作函数重写到 Db类中
SmileHoHo
Original
596 people have browsed it

数据库操作函数重写到 Db类中

代码:

  1. <?php
  2. class Db{
  3. protected $table = null;
  4. public function __construct($dsn,$user,$password,$table){
  5. $this-> pdo = new PDO($dsn,$user,$password);
  6. $this->table =$table;
  7. }
  8. public function create($data){
  9. $sql = "INSERT INTO {$this->table} SET";
  10. if(is_array($data)){
  11. foreach ($data as $k=>$v){
  12. $sql .=$k.'="'. $v.'",';
  13. }
  14. }else{
  15. return false;
  16. }
  17. $sql = rtrim(trim($sql),',').';';
  18. $stmt = $this->pdo->prepare($sql);
  19. }
  20. public function read($where,$order,$limit=10,$fields='*'){
  21. //创建SQL语句
  22. $sql = 'SELECT ';
  23. if (is_array($fields)) {
  24. foreach ($fields as $field) {
  25. $sql .= $field.', ';
  26. }
  27. } else {
  28. $sql .= $fields;
  29. }
  30. $sql = rtrim(trim($sql),',');
  31. $sql .= ' FROM '.$this->table;
  32. //查询条件
  33. if(!empty($where)){
  34. $sql .= ' WHERE '.$where;
  35. }
  36. //排序条件
  37. if(!empty($order)) {
  38. $sql .= ' order by '.$order;
  39. }
  40. //分页条件
  41. if(!empty($limit)) {
  42. $sql .= ' limit '.$limit;
  43. }
  44. $sql .= ';';
  45. //创建PDO预处理对象
  46. $stmt = $this->pdo->prepare($sql);
  47. //执行查询操作
  48. if($stmt->execute()){
  49. if($stmt->rowCount()>0){
  50. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  51. //返回一个二维数组
  52. return $stmt->fetchAll();
  53. }
  54. } else {
  55. return false;
  56. }
  57. }
  58. public function update($table,$data=[], $where=''){
  59. $sql = "UPDATE {$this->table} SET ";
  60. //组装修改语句
  61. if(is_array($data)){
  62. foreach ($data as $k=>$v) {
  63. $sql .= $k.'="'.$v.'", ';
  64. }
  65. }
  66. //去掉尾部逗号,并添加分号结束
  67. $sql = rtrim(trim($sql),',');
  68. //查询条件
  69. if(!empty($where)){
  70. $sql .= ' WHERE '.$where;
  71. }
  72. //创建PDO预处理对象
  73. $stmt = $this->pdo->prepare($sql);
  74. //执行新增操作
  75. if($stmt->execute()){
  76. if($stmt->rowCount()>0){
  77. return true;
  78. }
  79. } else {
  80. return false;
  81. }
  82. }
  83. public function delete($table,$where){
  84. $sql = "DELETE FROM {$this->table} ";
  85. //查询条件
  86. if(!empty($where)){
  87. $sql .= ' WHERE '.$where;
  88. }
  89. //创建PDO预处理对象
  90. $stmt = $this->pdo->prepare($sql);
  91. //执行删除操作
  92. if($stmt->execute()){
  93. if($stmt->rowCount()>0){
  94. return true;
  95. }
  96. } else {
  97. return false;
  98. }
  99. }
  100. }
  101. $d = new Db('mysql:host=localhost;dbname=php2020','root','roottt','user');
  102. $d->read('uid=1','age DESC');
  103. print_r($d);

手抄书:




Correcting teacher:查无此人查无此人

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