Blogger Information
Blog 36
fans 1
comment 0
visits 28750
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月27日重写数据库操作函数到类-九期线上班
WJF
Original
559 people have browsed it

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

  1. <?php
  2. namespace Db;
  3. class Db
  4. {
  5. public $dsn;
  6. public $name;
  7. public $pass;
  8. public $pdo;
  9. public function __construct($dsn,$name,$pass)
  10. {
  11. $this->dsn = $dsn;
  12. $this->name = $name;
  13. $this->pass = $pass;
  14. $this->connect();
  15. }
  16. public function connect(){
  17. try {
  18. $this->pdo = new \PDO($this->dsn, $this->name, $this->pass);
  19. echo '连接成功<hr>';
  20. } catch (\PDOException $s){
  21. die(print_r($s->getMessage(), true));
  22. }
  23. return $this->pdo;
  24. }
  25. //增加操作
  26. public function add($table,array $data = [])
  27. {
  28. $sql = "INSERT INTO {$table} SET ";
  29. // print_r($data);
  30. foreach ($data as $k => $v){
  31. $sql .= '`' . $k . '`=' . "'$v'" . ',';
  32. }
  33. $sql = rtrim(trim($sql),',') . ';';
  34. // echo $sql;
  35. //创建数据库预处理对象
  36. $sqlt = $this->pdo->prepare($sql);
  37. //执行操作
  38. // $sqlt->execute();
  39. if ($sqlt->execute()){
  40. echo '成功增加' . $sqlt->rowCount() . '条记录!' ;
  41. }else{
  42. echo '失败!';
  43. }
  44. }
  45. //更新操作
  46. public function update($table,$data = [],$where = '')
  47. {
  48. $sql = "UPDATE {$table} SET ";
  49. foreach ($data as $k => $v){
  50. $sql .= '`' . $k . '`=' . "'$v'" . ',';
  51. }
  52. $sql = rtrim(trim($sql),',');
  53. $sql .= 'WHERE' . $where;
  54. // echo $sql;
  55. //创建数据库预处理对象
  56. $sqlt = $this->pdo->prepare($sql);
  57. //执行操作
  58. // $sqlt->execute();
  59. if ($sqlt->execute()){
  60. echo '成功修改' . $sqlt->rowCount() . '条记录!' ;
  61. }else{
  62. echo '失败!';
  63. }
  64. }
  65. //删除操作
  66. public function delete($table,$where)
  67. {
  68. $sql = "DELETE FROM {$table} WHERE {$where}";
  69. // echo $sql;
  70. //创建数据库预处理对象
  71. $sqlt = $this->pdo->prepare($sql);
  72. //执行操作
  73. // $sqlt->execute();
  74. if ($sqlt->execute()){
  75. echo '成功删除' . $sqlt->rowCount() . '条记录!' ;
  76. }else{
  77. echo '失败!';
  78. }
  79. }
  80. //查询数据
  81. public function fetch ($table,$fie,$where)
  82. {
  83. $sql = "SELECT {$fie} FROM {$table} WHERE {$where};";
  84. //// echo $sql;
  85. // var_dump($fie);
  86. // var_dump($table);
  87. // var_dump($where);
  88. //创建数据库预处理对象
  89. $stmt = $this->pdo->prepare($sql);
  90. $find = $stmt->execute();
  91. if ($find) {
  92. return $stmt->fetch(PDO::FETCH_ASSOC);
  93. } else {
  94. return false;
  95. }
  96. }
  97. }
  98. $db = new Db('mysql:host=127.0.0.1;dbname=test1','root','root');
  99. // $db->add('category',['name'=>'ds','alias'=>'都市生活剧']);
  100. // $db->update('category',['name'=>'ds','alias'=>'都市生活剧'],'`cate_id`=8');
  101. // $db->delete('category','`cate_id`=14');
  102. $db->fetch('category','*','cate_id = 1');
  103. // echo $fetch;
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