Blogger Information
Blog 26
fans 1
comment 0
visits 22375
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 第课: 把 php公用方法库中的 数据库操作函数, 重写到 Db类中 11月27日
孤忆寻昔R
Original
994 people have browsed it

将公共方法封装Db类中

  1. <?php
  2. //公共方法库
  3. class Db
  4. {
  5. protected $table = null;
  6. protected $pdo;
  7. public function __construct($dsn,$user,$password,$table)
  8. {
  9. $this->pdo = new PDO($dsn,$user,$password);
  10. $this->table = $table;
  11. }
  12. // 查询方法
  13. //select * from biao WHERE 条件 order by 排序 limit 页
  14. public function select($where='',$order='',$limilt='0,10', $filed='*')
  15. {
  16. //select from 必须有
  17. $sql = 'SELECT ';
  18. if($filed == '*'){
  19. $sql .= '* FROM ';
  20. } else{
  21. $sql .= $filed.' FROM ';
  22. }
  23. $sql .= $this->table;
  24. if(!$where){
  25. $sql .= ' WHERE '.$where;
  26. }
  27. print_r($sql);
  28. if($order)
  29. {
  30. $sql .= ' ORDER BY '.$order;
  31. }
  32. print_r($sql);
  33. $stmt = $this->pdo->prepare($sql);
  34. if($stmt->execute())
  35. {
  36. if($stmt->rowCount()>0){
  37. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  38. return $stmt->fetchAll();
  39. }
  40. }else {
  41. return false;
  42. }
  43. $sql = 'SELECT FROM';
  44. }
  45. //插入数据
  46. public function insert($table,$data)
  47. {
  48. $sql = "INSERT INTO `{$table}` SET ";
  49. //组装插入语句
  50. if(is_array($data)){
  51. foreach ($data as $k=>$v) {
  52. $sql .= $k.'="'.$v.'", ';
  53. }
  54. }else{
  55. return false;
  56. }
  57. // print_r($sql);
  58. //去掉尾部逗号,并添加分号结束
  59. $sql = rtrim(trim($sql),',').';';
  60. //创建PDO预处理对象
  61. // print_r($sql);
  62. $stmt = $this->pdo->prepare($sql);
  63. //执行新增操作
  64. // print_r($stmt);
  65. if($stmt->execute()){
  66. if($stmt->rowCount()>0){
  67. $num = $stmt->rowCount();
  68. if($num){
  69. $id = $this->pdo->lastInsertId();
  70. echo "<h3>成功新增 {$num}了条记录.新增记录的主键id:{$id}</h3>";
  71. }
  72. }
  73. } else {
  74. return false;
  75. }
  76. // print_r($stmt);
  77. }
  78. // 更新操作
  79. function update($table,$data,$where='') {
  80. //创建SQL语句
  81. $sql = "UPDATE `{$table}` SET ";
  82. //组装修改语句
  83. if(is_array($data)){
  84. foreach ($data as $k=>$v) {
  85. $sql .= $k.'="'.$v.'", ';
  86. }
  87. }
  88. // print_r($sql);
  89. //去掉尾部逗号,并添加分号结束
  90. $sql = rtrim(trim($sql),',');
  91. //查询条件
  92. if(!empty($where)){
  93. $sql .= ' WHERE '.$where;
  94. }
  95. // print_r($sql);
  96. //创建PDO预处理对象
  97. $stmt = $this->pdo->prepare($sql);
  98. //执行新增操作
  99. if($stmt->execute()){
  100. if($stmt->rowCount()>0){
  101. $num = $stmt->rowCount();
  102. if($num){
  103. $id = $this->pdo->lastInsertId();
  104. echo "<h3>成功更新 {$num}了条记录</h3>";
  105. }
  106. }
  107. } else {
  108. return false;
  109. }
  110. }
  111. public function delete($table,$where){
  112. // 预处理执行删除操作
  113. $sql = 'DELETE FROM '.$table.' WHERE '.$where;
  114. $stmt = $this->pdo->prepare($sql);
  115. $stmt->execute();
  116. return $stmt->rowCount();
  117. }
  118. }
  119. //实例化对象 并向 构造方法传参
  120. $a = new Db('mysql:host=127.0.0.1;dbname=pdo','root','root','users');
  121. ////查询方法 查询多个记录
  122. //$selects = $a->select('id=4 AND id = 11','age DESC','0,10','id,username,password');
  123. //
  124. //print_r($selects);
  125. ////插入数据 单条
  126. //$insert = $a->insert('users',['username'=>'张三','password'=>sha1('123456'),'age'=>35]);
  127. //print_r($insert);
  128. //
  129. //$update = $a ->update('users',['username'=>'老黄','password'=>sha1(123456),'age'=>'18'],"id=153");
  130. //print_r($update);
  131. // 删除记录
  132. $where = 'id >= 150';
  133. echo '成功删除了: ' .$a->delete('users',$where). ' 条记录';




总结:

1、操作增、删、改,写操作时一定要注意,没控制好就是表全部删除,或者是修改!

2、经过封装后 ,方法传什么参数,方对应的值!

3、在封装时感觉自己没有什么方法完成,都是疯狂的试探测试,有的时候好像问老师,但最终还是忍下来,必须自己完成

4、由于27日和29日的作业差不多,所以完成很像

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