博客列表 >手敲PHP抽像类及接口案例、用类实现数据CURD-第九期(191129作业)

手敲PHP抽像类及接口案例、用类实现数据CURD-第九期(191129作业)

feng
feng原创
2019年12月01日 21:41:55745浏览

手敲PHP抽像类及接口案例:

  1. <?php
  2. interface iCurd{
  3. public function create($data);
  4. public function read();
  5. public function update($data,$where);
  6. public function delete($where);
  7. }
  8. class Db implements iCurd{
  9. protected $pdo=null;
  10. protected $table;
  11. public function __construct($dsn,$user,$password,$table='staff')
  12. {
  13. $this->pdo=new PDO($dsn,$user,$password);
  14. $this->table=$table;
  15. }
  16. public function read($fields='*',$where='',$limit='0,5')
  17. {
  18. // TODO: Implement read() method.
  19. $where=empty($where)?'':' WHERE '.$where;
  20. $limit=' LIMIT '.$limit;
  21. $sql='SELETE '.$fields.' FROM '.$this->table.$where.$limit;
  22. $stsm=$this->pdo->prepare($sql);
  23. $stsm->execute();
  24. return $stsm->fetchAll(PDO::FETCH_ASSOC);
  25. }
  26. public function create($data)
  27. {
  28. // TODO: Implement create() method.
  29. $fields=' (name,age,sex,position,mobile,hiredate)';
  30. $values='(:name,:age,:sex,:position,:mobile,:hiredate)';
  31. $sql='INSERT INTO '.$this->table.$fields.' VALUES '.$values;
  32. $stsm=$this->pdo->prepare($sql);
  33. $stsm->execute($data);
  34. return [
  35. 'count'=>$stsm->rowCount(),
  36. 'id'=>$this->pdo->lastInsertId()
  37. ];
  38. }
  39. public function update($data, $where)
  40. {
  41. // TODO: Implement update() method.
  42. $keyArr=array_keys($data);
  43. $set='';
  44. foreach ($keyArr as $value){
  45. $set.=$value.' =:'.$value.', ';
  46. }
  47. $set=trim(rtrim($set,','));
  48. $sql='UPDATE '.$this->table.' SET '.$set.' WHERE '.$where;
  49. $stsm=$this->pdo->prepare($set);
  50. $stsm->execute($data);
  51. return $stsm->rowCount();
  52. }
  53. public function delete($where)
  54. {
  55. // TODO: Implement delete() method.
  56. $sql='DELETE FROM '.$this->table.' WHERE '.$where;
  57. $stsm=$this->pdo->prepare($sql);
  58. $stsm->execute();
  59. return $stsm->rowCount();
  60. }
  61. }
  62. $dsn='mysql:host=localhost;dbname=ouk';
  63. $user='root';
  64. $password='123456';
  65. $db=new Db($dsn,$user,$password);
  66. foreach ($db->read() as $item){
  67. print_r($item);
  68. echo '<br>';
  69. }
  70. echo '<hr>';
  71. $data=[
  72. 'name'=>'郭靖',
  73. 'age'=>30,
  74. 'sex'=>1,
  75. 'position'=>'金刀附马',
  76. 'mobile'=>1388888888,
  77. 'hiretime'=>time()
  78. ];
  79. $res=$db->create($data);
  80. echo '成功新增'.$res['count'].'条记录,最新记录的主键是:'.$res['id'];
  81. echo '<hr>';
  82. $data=[
  83. 'age'=>5,
  84. 'position'=>'抗金英雄'
  85. ];
  86. $where='id=5';
  87. echo '成功更新了:'.$db->update($data,$where).'条记录';
  88. echo '<hr>';
  89. $where='id=5';
  90. echo '成功删除了'.$db->delete($where).'条记录';

二、用类实现数据库CURD

  1. <?php
  2. class Db{
  3. public $dsn;
  4. public $user;
  5. public $password;
  6. public $pdo=null;
  7. public function __construct($dsn,$user,$password)
  8. {
  9. $this->dsn=$dsn;
  10. $this->user=$user;
  11. $this->password=$password;
  12. $this->connect();
  13. }
  14. public function connect(){
  15. $this->pdo=new PDO($this->dsn,$this->user,$this->password);
  16. }
  17. public function select($table,$fields='*',$where='',$order='',$limit=''){
  18. $sql='SELECT ';
  19. if (is_array($fields)){
  20. foreach ($fields as $field){
  21. $sql=$sql.$field.',';
  22. }
  23. $sql=rtrim(trim($sql),',');
  24. }else{
  25. $sql.=$fields;
  26. }
  27. $sql.=' FROM '.$table;
  28. // die($sql);
  29. if (!is_null($where)){
  30. $sql.=' WHERE '.$where;
  31. }
  32. // die($sql);
  33. if(!is_null($order)){
  34. $sql.=' order by '.$order;
  35. }
  36. if(!is_null($limit)){
  37. $sql.=' limit '.$limit;
  38. }
  39. // var_dump($this->pdo);die;
  40. $stsm=$this->pdo->prepare($sql);
  41. if($stsm->execute()){
  42. if($stsm->rowCount()>0){
  43. $stsm->setFetchMode(PDO::FETCH_ASSOC);
  44. return $stsm->fetchAll();
  45. }
  46. }
  47. return false;
  48. }
  49. public function insert($table,$data=[]){
  50. $sql='insert into '.$table.' set ';
  51. if (is_array($data)){
  52. foreach ($data as $field => $value){
  53. $sql.=($field.'=\''.$value.'\',');
  54. }
  55. $sql=rtrim(trim($sql),',');
  56. $stsm=$this->pdo->prepare($sql);
  57. if($stsm->execute()){
  58. if($stsm->rowCount()>0){
  59. return '插入的ID号为'.$this->pdo->lastInsertID();
  60. }else{
  61. return false;
  62. }
  63. }else{
  64. return false;
  65. }
  66. }
  67. return false;
  68. }
  69. public function update($table,$where=[],$set=[]){
  70. $sql="update {$table} set ";
  71. if(is_array($set)){
  72. foreach ($set as $field => $value){
  73. $sql.=($field.'=\''.$value.'\',');
  74. }
  75. $sql=rtrim(trim($sql),',');
  76. // die($sql);
  77. }else{
  78. return false;
  79. }
  80. if (is_array($where)){
  81. $sql.=' where ';
  82. foreach ($where as $field => $value){
  83. $sql.=($field.' = \''.$value.'\'' . ' and ');
  84. }
  85. $sql=rtrim(trim($sql),' and ');
  86. $stsm=$this->pdo->prepare($sql);
  87. if ($stsm->execute()){
  88. if ($stsm->rowcount()>0){
  89. return '修改记录数为:'.$stsm->rowcount();
  90. }else{
  91. return false;
  92. }
  93. }else{
  94. return false;
  95. }
  96. }else{
  97. return false;
  98. }
  99. }
  100. public function delete($table,$where=[]){
  101. $sql="delete from {$table} where ";
  102. if (is_array($where)){
  103. foreach ($where as $field => $value){
  104. $sql.=($field.' = \''.$value.'\'' . ' and ');
  105. }
  106. $sql=rtrim(trim($sql),' and ');
  107. // die($sql);
  108. $stsm=$this->pdo->prepare($sql);
  109. if ($stsm->execute()){
  110. if ($stsm->rowcount()>0){
  111. return '删除记录数为:'.$stsm->rowcount();
  112. }else{
  113. return false;
  114. }
  115. }else{
  116. return false;
  117. }
  118. }else{
  119. return false;
  120. }
  121. }
  122. public function count_num($table){
  123. $sql='select count(*) as total from '.$table;
  124. $stsm=$this->pdo->prepare($sql);
  125. if ($stsm->execute()){
  126. if ($stsm->rowCount()>0){
  127. $total=$stsm->fetchColumn();
  128. return "表{$table}总共有记录数{$total}条";
  129. }else{
  130. return false;
  131. }
  132. }else{
  133. return false;
  134. }
  135. }
  136. }
  137. $dsn='mysql:host=localhost;dbname=emshop';
  138. $obj=new Db($dsn,'root',123456);
  139. //print_r($obj->select('movies',['mov_id','name','cate_id'],'mov_id>3','mov_id desc','4'));
  140. //echo $obj->insert('category',['name'=>'aa','alias'=>'bb']);
  141. //echo $obj->update('category',['cate_id'=>'9','name'=>'aa'],['name'=>'cc','alias'=>'dd']);
  142. //echo $obj->delete('category',['cate_id'=>'9','name'=>'aa']);
  143. echo $obj->count_num('movies');
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议