Blogger Information
Blog 19
fans 1
comment 0
visits 13305
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
2019.11.27把 php公用方法库中的 数据库操作函数, 重写到 Db类中php培训第九期线上班
涤尘
Original
876 people have browsed it

把 php公用方法库中的 数据库操作函数, 重写到 Db类中

  1. <?php
  2. class Db{
  3. public $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语句
  10. $sql = "INSERT INTO {$this->table} SET ";
  11. //组装插入语句
  12. if(is_array($data)){
  13. foreach ($data as $k=>$v) {
  14. $sql .= $k.'="'.$v.'", ';
  15. }
  16. }else{
  17. return false;
  18. }
  19. //去掉尾部逗号,并添加分号结束
  20. $sql = rtrim(trim($sql),',').';';
  21. //创建PDO预处理对象
  22. $stmt = $this->pdo->prepare($sql);
  23. //执行新增操作
  24. if($stmt->execute()){
  25. if($stmt->rowCount()>0){
  26. return true;
  27. }
  28. } else {
  29. return false;
  30. }
  31. }
  32. public function read($where,$order,$limit=10,$fields='*') {
  33. //创建SQL语句
  34. $sql = 'SELECT ';
  35. if (is_array($fields)) {
  36. foreach ($fields as $field) {
  37. $sql .= $field.', ';
  38. }
  39. } else {
  40. $sql .= $fields;
  41. }
  42. $sql = rtrim(trim($sql),',');
  43. $sql .= ' FROM '.$this->table;
  44. //查询条件
  45. if(!empty($where)){
  46. $sql .= ' WHERE '.$where;
  47. }
  48. //排序条件
  49. if(!empty($order)) {
  50. $sql .= ' order by '.$order;
  51. }
  52. //分页条件
  53. if(!empty($limit)) {
  54. $sql .= ' limit '.$limit;
  55. }
  56. $sql .= ';';
  57. //创建PDO预处理对象
  58. $stmt = $this->pdo->prepare($sql);
  59. //执行查询操作
  60. if($stmt->execute()){
  61. if($stmt->rowCount()>0){
  62. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  63. //返回一个二维数组
  64. return $stmt->fetchAll();
  65. }
  66. } else {
  67. return false;
  68. }
  69. }
  70. public function update($table,$data=[],$where) {
  71. $sql = "UPDATE {$this->table} SET ";
  72. //组装修改语句
  73. if(is_array($data)){
  74. foreach ($data as $k=>$v) {
  75. $sql .= $k.'="'.$v.'", ';
  76. }
  77. }
  78. //去掉尾部逗号,并添加分号结束
  79. $sql = rtrim(trim($sql),',');
  80. //查询条件
  81. if(!empty($where)){
  82. $sql .= ' WHERE '.$where;
  83. }
  84. //创建PDO预处理对象
  85. $stmt = $this->pdo->prepare($sql);
  86. //执行新增操作
  87. if($stmt->execute()){
  88. if($stmt->rowCount()>0){
  89. return true;
  90. }
  91. } else {
  92. return false;
  93. }
  94. }
  95. public function delete($table,$where) {
  96. $sql = "DELETE FROM {$this->table} ";
  97. //查询条件
  98. if(!empty($where)){
  99. $sql .= ' WHERE '.$where;
  100. }
  101. //创建PDO预处理对象
  102. $stmt = $this->pdo->prepare($sql);
  103. //执行删除操作
  104. if($stmt->execute()){
  105. if($stmt->rowCount()>0){
  106. return true;
  107. }
  108. } else {
  109. return false;
  110. }
  111. }
  112. }
  113. $d = new Db('mysql:host=localhost;dbname=mysite','root','root','movies');
  114. $d->read('mov_id=1','cate_id DESC');
  115. 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
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!