博客列表 >02月11日作业:MySQLi数据操作和多态接口

02月11日作业:MySQLi数据操作和多态接口

李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰原创
2020年02月14日 20:45:01757浏览

作业一

mysqli相关知识:

作业二

mysqli常见的CURD操作
1增加数据:
1、代码:

  1. <?php
  2. namespace part1;
  3. use mysqli;
  4. //连接数据库
  5. $mysql=new mysqli('NewYear.com','NY','123456','newyear.com');
  6. $stmt=$mysql->stmt_init();
  7. $sql="INSERT site_info SET 类型=?,优先级=?,标题=?,url=?,image=?,分类=?,标签=?;";
  8. $data=['测试1',1,'测试标题测试标题1','http://newyear.com','image/001.jpg','无','测试1'];
  9. list($type,$order,$site,$url,$image,$class,$tag)=$data;
  10. $stmt->prepare($sql);
  11. $stmt->bind_param('sisssss',$type,$order,$site,$url,$image,$class,$tag);
  12. $stmt->execute();
  13. if ($stmt->affected_rows===1):
  14. echo '添加记录成功,新的主键ID:'.$stmt->insert_id;
  15. else:
  16. echo '添加失败'.$stmt->error;
  17. endif;
  18. $stmt->close();

2、效果图


2、更新数据:
跟新和怎加基本一致只是SQL语句不同,另外mysqli_stmt类中insert_id();在更新下无法使用。
1、核心代码:

  1. $stmt=$mysql->stmt_init();
  2. $sql="UPDATE `site_info` SET `标题`=? WHERE id=? ";
  3. $stmt->prepare($sql);
  4. $data=['更新测试'62];
  5. list($site,$id)=$data;
  6. $stmt->bind_param('si',$site,$id);

2、效果图


3、删除数据
1、核心代码:

  1. $sql='DELETE FROM `site_info` WHERE `id`=?';
  2. $stmt->prepare($sql);
  3. $id=61;
  4. $stmt->bind_param('i',$id);

2、效果图


4、查询操作
1、用mysqli_stmt类查询:
(1)代码:

  1. <?php
  2. namespace part1;
  3. use mysqli;
  4. $mysql=new mysqli('NewYear.com','NY','123456','newyear.com');
  5. $stmt=$mysql->stmt_init();
  6. $sql='SELECT `id`,`类型`,`优先级`,`标题`,`url`,`image`,`分类`,`标签` FROM `site_info` WHERE `id`>=?';
  7. $stmt->prepare($sql);
  8. $id=59;
  9. $stmt->bind_param('i',$id);
  10. $stmt->execute();
  11. $stmt->bind_result($id,$type,$order,$site,$url,$image,$class,$tag);
  12. //$stmt->fetch();
  13. while($stmt->fetch()):
  14. echo $id,$type,$order,$site,$url,$image,$class,$tag.'<br>';
  15. endwhile;
  16. $stmt->free_result();
  17. $stmt->close();

(2)、效果图:

2、用mysqli_result类查询:
(1)、代码:

  1. <?php
  2. namespace part1;
  3. use mysqli;
  4. //连接数据库
  5. $mysql=new mysqli('NewYear.com','NY','123456','newyear.com');
  6. //mysqli_stmt类初始化
  7. $stmt=$mysql->stmt_init();
  8. //准备sql语句
  9. $sql='SELECT `id`,`类型`,`优先级`,`标题`,`url`,`image`,`分类`,`标签` FROM `site_info` WHERE `id`>?';
  10. //预处理
  11. $stmt->prepare($sql);
  12. $id=57;
  13. //绑定数据
  14. $stmt->bind_param('i',$id);
  15. //执行
  16. $stmt->execute();
  17. //获取结果返回mysql_result类实例
  18. $result=$stmt->get_result();
  19. //获取数据
  20. //$log=$result->fetch_array();
  21. //$log=$result->fetch_row();
  22. //$log=$result->fetch_assoc();
  23. $logs=$result->fetch_all(MYSQLI_ASSOC);
  24. //处理输出数据
  25. foreach ($logs as $log):
  26. echo '<pre>'.print_r($log,true).'</pre>';
  27. endforeach;
  28. //echo print_r($log,true);
  29. //$stmt->bind_result($id,$type,$order,$site,$url,$image,$class,$tag);
  30. //while($stmt->fetch()):
  31. // echo $id,$type,$order,$site,$url,$image,$class,$tag.'<br>';
  32. //endwhile;
  33. //释放结果集
  34. $result->free_result();
  35. //关闭数据库
  36. $stmt->close();

(2)、效果图

作业三

1、类接口:

  1. <?php
  2. namespace part1;
  3. //数据库连接常量
  4. interface iDbParam
  5. {
  6. const HOST='NewYear.com';
  7. const TYPE='mysql';
  8. const DBNAME='newyear.com';
  9. const USER_NAME='root';
  10. const PASSWORD='root';
  11. const CHARSET='utf8';
  12. const PORT='3306';
  13. }
  14. //构造接口方法
  15. interface iDbLink
  16. {
  17. public function __construct(...$linkParams);
  18. }
  19. interface iCURD extends iDbParam,iDbLink
  20. {
  21. public function insert(array $data);
  22. public function update(array $data, string $where);
  23. public function delete(string $where);
  24. public function select(string $where);
  25. }

(2)PDO方法代码:

  1. <?php
  2. namespace part1;
  3. use PDO;
  4. require '0211-1.php';
  5. class Db_PDO implements iCURD
  6. {
  7. private $pdo=null;
  8. public function __construct(...$linkParams)
  9. {
  10. list($dsn,$username,$password)=$linkParams;
  11. $this->pdo=new PDO($dsn,$username,$password);
  12. }
  13. public function insert(array $data)
  14. {
  15. $sql='INSERT site_info SET 类型=?,优先级=?,标题=?,url=?,image=?,分类=?,标签=?;';
  16. $stmt=$this->pdo->prepare($sql);
  17. $stmt->execute($data);
  18. return $stmt->rowCount()===1 ? '新增成功' : '新增失败';
  19. }
  20. public function update(array $data, string $where)
  21. {
  22. $params='';
  23. foreach ($data as $key=>$value){
  24. $params.="`{$key}`='{$value}',";
  25. }
  26. $params=rtrim($params,',');
  27. $sql="UPDATE `site_info` SET {$params} WHERE {$where}";
  28. // die($sql);
  29. $stmt=$this->pdo->prepare($sql);
  30. // die($stmt);
  31. $stmt->execute();
  32. return $stmt->rowCount()===1 ? '更新成功' : '更新失败';
  33. }
  34. public function delete(string $where)
  35. {
  36. $sql="DELETE FROM `site_info` WHERE {$where}";
  37. // die($sql);
  38. $stmt=$this->pdo->prepare($sql);
  39. $stmt->execute();
  40. return $stmt->rowCount()===1 ? '删除成功' : '删除失败';
  41. }
  42. public function select(string $where)
  43. {
  44. $where= empty($where) ? $where : 'WHERE '.$where;
  45. $sql="SELECT * FROM `site_info` {$where}";
  46. // die($sql);
  47. $stmt = $this->pdo->prepare($sql);
  48. // die($stmt);
  49. $stmt->execute();
  50. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  51. }
  52. }

(3)MySQLi方法代码:

  1. <?php
  2. namespace part1;
  3. require '0211-1.php';
  4. use mysqli;
  5. class MySQL implements iCURD
  6. {
  7. private $mysqli = null;
  8. public function __construct(...$linkParams)
  9. {
  10. list($host,$username,$password,$dbname)=$linkParams;
  11. $this->mysqli=new mysqli($host,$username,$password,$dbname);
  12. $this->mysqli->set_charset('utf8');
  13. }
  14. public function insert(array $data){
  15. $sql='INSERT site_info SET 类型=?,优先级=?,标题=?,url=?,image=?,分类=?,标签=?;';
  16. $stmt=$this->mysqli->stmt_init();
  17. $stmt->prepare($sql);
  18. list($type,$order,$site,$url,$image,$class,$tag)=$data;
  19. $stmt->bind_param('sisssss',$type,$order,$site,$url,$image,$class,$tag);
  20. $stmt->execute();
  21. return $stmt->affected_rows===1 ? '增加成功' : '增加失败';
  22. }
  23. public function update(array $data, string $where)
  24. {
  25. $params='';
  26. foreach ($data as $key=>$value){
  27. $params.="`{$key}`='{$value}',";
  28. }
  29. $params=rtrim($params,',');
  30. $sql="UPDATE `site_info` SET {$params} WHERE {$where}";
  31. // die($sql);
  32. $stmt=$this->mysqli->stmt_init();
  33. $stmt->prepare($sql);
  34. $stmt->execute();
  35. return $stmt->affected_rows===1 ? '更新成功' : '更新失败';
  36. }
  37. public function delete(string $where)
  38. {
  39. $sql="DELETE FROM `site_info` WHERE {$where}";
  40. $stmt=$this->mysqli->stmt_init();
  41. $stmt->prepare($sql);
  42. $stmt->execute();
  43. return $stmt->affected_rows===1 ? '删除成功' : '删除失败';
  44. }
  45. public function select(string $where)
  46. {
  47. $where= empty($where) ? $where : 'WHERE '.$where;
  48. $sql="SELECT * FROM `site_info`".$where;
  49. $stmt=$this->mysqli->stmt_init();
  50. $stmt->prepare($sql);
  51. $stmt->execute();
  52. return $stmt->get_result();
  53. }
  54. }

(4)通用类:数据库操作:

  1. <?php
  2. namespace part1;
  3. use part1\iCURD;
  4. class DB
  5. {
  6. public static function insert(iCURD $db, array $data)
  7. {
  8. return $db->insert($data);
  9. }
  10. public static function update(iCURD $db, array $data,string $where)
  11. {
  12. return $db->update($data,$where);
  13. }
  14. public static function delete(iCURD $db,string $where)
  15. {
  16. return $db->delete($where);
  17. }
  18. public static function select(iCURD $db,string $where)
  19. {
  20. return $db->select($where);
  21. }
  22. }

(5)操作代码:
a、pdo操作数据库:

  1. <?php
  2. //PDO操作数据库
  3. namespace part1;
  4. require '0211-2.php';
  5. require '0211-3.php';
  6. $dsn=$dsn = iDbParam::TYPE . ':host='.'NewYear.com'. ';dbname='.'newyear.com' . ';charset='.iDbParam::CHARSET;
  7. $link=new Db_PDO($dsn,'NY','123456');
  8. //echo DB::insert($link,['测试0211',1,'测试标题测试标题0211','http://newyear.com','image/001.jpg','无','测试1']);
  9. //echo DB::update($link,['类型'=>'PDO测试'],'`id`=62');
  10. //echo DB::delete($link,'id=64');
  11. foreach (DB::select($link,'id>54') as $arr)
  12. {
  13. echo '<pre>'.print_r($arr,true).'</pre>>';
  14. }

b、mysqli操作数据库:

  1. <?php
  2. namespace part1;
  3. require '0211-21.php';
  4. require '0211-3.php';
  5. $link = new MySQL('NewYear.com', 'NY', '123456','newyear.com');
  6. //echo DB::insert($link,['测试mysqli',1,'测试标题测试标题0211','http://newyear.com','image/001.jpg','无','测试1']);
  7. //echo DB::update($link,['类型'=>'PDO-测试'],'`id`=62');
  8. //echo DB::delete($link,'id=63');
  9. $result=DB::select($link,'id=59');
  10. $results=$result->fetch_all(MYSQLI_ASSOC);
  11. echo '<pre>'.print_r($results,true).'</pre>';

四:补充

mysql_result:
$result->num_rows:返回结果的记录行数;

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议