Blogger Information
Blog 33
fans 0
comment 0
visits 19512
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月26日作业数据库操作函数重写到 Db类中--PHP培训九期线上班
取个名字真难
Original
518 people have browsed it

用到两个文件

connect.php数据库链接相关的文件

  1. <?php
  2. class con{
  3. // 需要用到的属性
  4. public $host;
  5. public $user;
  6. public $pass;
  7. public $name;
  8. public $type;
  9. public $port;
  10. public $charset;
  11. public $dsn;
  12. // 创建链接数据库的方法construct
  13. public function construct($host,$user,$pass,$name,$type='mysql',$port=3306,$charset='utf8' ){
  14. $this->host=$host;
  15. $this->user=$user;
  16. $this->pass=$pass;
  17. $this->name=$name;
  18. $this->type=$type;
  19. $this->port=$port;
  20. $this->charset=$charset;
  21. $db = array(
  22. 'charset' => $this->charset,
  23. 'port' => $this->port,
  24. 'type' => $this->type,
  25. 'host' => $this->host,
  26. 'user' => $this->user,
  27. 'pass' => $this->pass,
  28. 'name' => $this->name
  29. );
  30. $dsn = "{$db['type']}:host={$db['host']}; dbname={$db['name']}; charset={$db['charset']}; port={$db['port']}";//数据源
  31. try {
  32. //实例化PDO类,创建PDO对象
  33. $pdo = new PDO($dsn,$db['user'],$db['pass']);
  34. } catch (PDOException $e) {
  35. die('数据库错误:'.$e->getMessage());
  36. }
  37. return $pdo;
  38. }
  39. }
  40. //实例化父类的对象
  41. $c1=new con();
  42. // 链接数据库
  43. $pdo=$c1->construct('127.0.0.1','root','root','dedecmsv57');

pdoclass.php文件

  1. <?php
  2. //引入connect数据库链接文件中的con类
  3. require __DIR__.'/connect.php';
  4. //创建con的子类find
  5. class find extends con{
  6. public $table;
  7. public $fields;
  8. public $where;
  9. public $order;
  10. public $limit;
  11. //创建子类的查询find
  12. public function select($table,$fields, $where='', $order='',$limit=''){
  13. $this->table = $table;
  14. $this->fields=$fields;
  15. $this->where=$where;
  16. $this->order=$order;
  17. $this->limit=$limit;
  18. //实例化父类的对象
  19. $c1=new con();
  20. // 链接数据库
  21. $pdo=$c1->construct('127.0.0.1','root','root','dedecmsv57');
  22. //创建SQL语句
  23. $sql = 'SELECT ';
  24. if (is_array($fields)) {
  25. foreach ($fields as $field) {
  26. $sql .= $field.', ';
  27. }
  28. } else {
  29. $sql .= $fields;
  30. }
  31. $sql = rtrim(trim($sql),',');
  32. $sql .= ' FROM '.$table;
  33. //查询条件
  34. if(!empty($where)){
  35. $sql .= ' WHERE '.$where;
  36. }
  37. //排序条件
  38. if(!empty($order)) {
  39. $sql .= ' order by '.$order;
  40. }
  41. //分页条件
  42. if(!empty($limit)) {
  43. $sql .= ' limit '.$limit;
  44. }
  45. $sql .= ';';
  46. //创建PDO预处理对象
  47. $stmt = $pdo->prepare($sql);
  48. //执行查询操作
  49. if($stmt->execute()){
  50. if($stmt->rowCount()>0){
  51. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  52. //返回一个二维数组
  53. return $stmt->fetchAll();
  54. }
  55. } else {
  56. return false;
  57. }
  58. }
  59. // 查询单条记录
  60. public function find2($table,$fields,$where=''){
  61. //实例化父类的对象
  62. $c1=new con();
  63. // 链接数据库
  64. $pdo=$c1->construct('127.0.0.1','root','root','dedecmsv57');
  65. $sql = 'SELECT ';
  66. if (is_array($fields)) {
  67. foreach ($fields as $field) {
  68. $sql .= $field.', ';
  69. }
  70. } else {
  71. $sql .= $fields;
  72. }
  73. $sql = rtrim(trim($sql),',');
  74. $sql .= ' FROM '.$table;
  75. //查询条件
  76. if(!empty($where)){
  77. $sql .= ' WHERE '.$where;
  78. }
  79. $sql .= ' LIMIT 1;';
  80. //创建PDO预处理对象
  81. $stmt = $pdo->prepare($sql);
  82. //执行查询操作
  83. if($stmt->execute()){
  84. if($stmt->rowCount()>0){
  85. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  86. return $stmt->fetch();
  87. }
  88. } else {
  89. return false;
  90. }
  91. }
  92. //* 新增数据
  93. public function insert($table,$data=[]){
  94. //实例化父类的对象
  95. $c1=new con();
  96. // 链接数据库
  97. $pdo=$c1->construct('127.0.0.1','root','root','dedecmsv57');
  98. $sql = "INSERT INTO {$table} SET ";
  99. //组装插入语句
  100. if(is_array($data)){
  101. foreach ($data as $k=>$v) {
  102. $sql .= $k.'="'.$v.'", ';
  103. }
  104. }else{
  105. return false;
  106. }
  107. //去掉尾部逗号,并添加分号结束
  108. $sql = rtrim(trim($sql),',').';';
  109. //创建PDO预处理对象
  110. $stmt = $pdo->prepare($sql);
  111. //执行新增操作
  112. if($stmt->execute()){
  113. if($stmt->rowCount()>0){
  114. return true;
  115. }
  116. } else {
  117. return false;
  118. }
  119. }
  120. //* 统计数量
  121. public function count_num($table,$where=''){
  122. //实例化父类的对象
  123. $c1=new con();
  124. // 链接数据库
  125. $pdo=$c1->construct('127.0.0.1','root','root','dedecmsv57');
  126. $sql = 'SELECT count(*) as count_number FROM '.$table;
  127. //查询条件
  128. if(!empty($where)){
  129. $sql .= ' WHERE '.$where;
  130. }
  131. //创建PDO预处理对象
  132. $stmt = $pdo->prepare($sql);
  133. //执行查询操作
  134. if($stmt->execute()){
  135. if($stmt->rowCount()>0){
  136. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  137. $rows = $row['count_number'];
  138. return $rows;
  139. }
  140. } else {
  141. return false;
  142. }
  143. }
  144. }
  145. //实例化对象
  146. $s1=new find();
  147. //查询到的数据是一个二维数组
  148. $find1=$s1->select('dede_addonarticle','aid,typeid,userip,body');
  149. //输出二维数组的body字段
  150. foreach ($find1 as $v){
  151. echo $v['userip'].'<br>';
  152. echo $v['body'].'<br>';
  153. }
  154. // 查询单条记录
  155. $value=$s1->find2('dede_addonarticle','aid,typeid,userip,body');
  156. echo $value['body'];
  157. //* 更新数据
  158. $s1->insert('dede_addonarticle','[1,20,"body里的文章内容比较多","我也不知道这是啥","这也不知道","2"]');
  159. //* 统计数量
  160. $a=$s1->count_num('dede_addonarticle');
  161. echo $a;

以上方法都能运行出来

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!