Blogger Information
Blog 42
fans 5
comment 0
visits 38612
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php class类 PDO 增删查改
张浩刚
Original
903 people have browsed it
  1. <?php
  2. class NB{
  3. function __construct($a,$b,$c,$table)
  4. {
  5. //pdo连接数据库
  6. $this->pdo = new PDO($a,$b,$c);
  7. //需要增删查改的表明
  8. $this->table = $table;
  9. }
  10. //查询
  11. function select($where = '', $limit = 6){
  12. $where = empty($where) ? '' : ' WHERE '.$where;
  13. $limit = ' LIMIT ' . $limit;
  14. $sql = 'SELECT * FROM ' .$this->table. $where . $limit;
  15. $stmt = $this->pdo->prepare($sql);
  16. $stmt->execute();
  17. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  18. }
  19. //新增
  20. function insert($data){
  21. //数据表字段列表
  22. $a = '(name,tel,password) ';
  23. //字段值列表
  24. $b = '(:name,:tel,:password)';
  25. //创建sql语句模板
  26. $sql = 'INSERT INTO ' . $this->table . $a .' VALUES' .$b;
  27. //预处理新增操作
  28. $stmt = $this->pdo->prepare($sql);
  29. $stmt->execute($data);
  30. //返回新增数量,及其主键id
  31. return [
  32. 'count' => $stmt->rowCount(),
  33. 'id' => $this->pdo->lastInsertId()
  34. ];
  35. }
  36. //更新
  37. function update($upda,$upid){
  38. //通过外部数组索引数组
  39. $keyArr = array_keys($upda);
  40. $set = '';
  41. //循环外部索引数组,获得如name=:name
  42. foreach($keyArr as $vv){
  43. // .=能让 $set 获取所有数组,若只有 = ,则只能获取只有一个数组
  44. $set .= $vv . '=:'.$vv . ', ';
  45. }
  46. $set = rtrim($set, ', ');
  47. echo $set;
  48. $sql = 'UPDATE '.$this->table.' SET '.$set.' WHERE ' .$upid;
  49. $stmt = $this->pdo->prepare($sql);
  50. $stmt->execute($upda);
  51. return $stmt->rowCount();
  52. }
  53. //删除
  54. function delete($deid){
  55. $sql = 'DELECT FROM '.$this->table.' WHERE '. $deid;
  56. $stmt = $this->pdo->prepare($sql);
  57. $stmt->execute();
  58. return $stmt->rowCount();
  59. }
  60. }
  61. $db = new NB('mysql:host=localhost;dbname=film', 'root', 'root', 'user');
  62. echo print_r($db->pdo);
  63. echo '<hr>';
  1. //查询结果
  2. foreach($db->select() as $v){
  3. print_r($v);
  4. }
  5. echo '<hr>';
  1. //新增
  2. $data = [
  3. 'name'=> '轩轩宝贝',
  4. 'tel'=>211899,
  5. 'password'=>sha1(123456)
  6. ];
  7. $inss = $db->insert($data);
  8. echo '成功新增'.$inss['count'].',它的主键id:'. $inss['id'];
  9. echo '<hr>';
  1. //更新
  2. $upda = [
  3. 'name' => '轩轩宝贝',
  4. 'tel'=> 15566554466
  5. ];
  6. $upid = 'id=14';
  7. echo '成功更新了' . $db->update($upda,$upid).'条信息';
  8. echo '<hr>';
  1. //删除
  2. $deid = 'id=12';
  3. echo '成功删除:' .$db->delete($deid). '条信息';
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