Blogger Information
Blog 25
fans 1
comment 1
visits 17506
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0211作业+mysqli操作数据库与接口实战+10期线上班
江川林
Original
563 people have browsed it

MYSQLI操作数据库

以下是增加操作

  1. <?php
  2. namespace mysql;
  3. use mysqli;
  4. //添加
  5. //用mysqli来进行数据库操作
  6. //链接数据库
  7. $mysqli = new mysqli('localhost','root','root','phpedu');
  8. //数据库操作
  9. //初始化返回预处理对象
  10. $stmt = $mysqli->stmt_init();
  11. // 准备SQL执行语句
  12. $sql = "INSERT staffs SET name=?,age=?,sex=?,position=?,mobile=?,hiredate=?" ;
  13. //创建sql语句对象
  14. $stmt->prepare($sql);
  15. //var_dump($stmt);
  16. //绑定参数
  17. $user = ['小江',23,1,'学生',1111111,2222222];
  18. list($name,$age,$sex,$position,$mobile,$hiredate) = $user;
  19. $stmt->bind_param('siisii',$name,$age,$sex,$position,$mobile,$hiredate);
  20. //执行
  21. $stmt->execute();
  22. //处理执行结果
  23. if($stmt->affected_rows === 1){
  24. echo '添加成功,'. '受影响的条数为:'. $stmt->affected_rows;
  25. }else {
  26. echo '添加失败'.$stmt->error;
  27. }
  28. //关闭数据库
  29. $mysqli->close();

以下是更改操作

  1. <?php
  2. namespace mysql;
  3. use mysqli;
  4. //更新/改
  5. //用mysqli来进行数据库操作
  6. //链接数据库
  7. $mysqli = new mysqli('localhost','root','root','phpedu');
  8. //数据库操作
  9. //初始化返回预处理对象
  10. $stmt = $mysqli->stmt_init();
  11. // 准备SQL执行语句
  12. //$sql = 'UPDATE `staffs` SET `name`=?,`age`=? WHERE `id`=?';
  13. $sql = 'UPDATE `staffs` SET `name`=?,`age`=? WHERE `id`>=?';
  14. //创建sql语句对象
  15. $stmt->prepare($sql);
  16. //var_dump($stmt);
  17. //绑定参数
  18. $user = ['小王',20,14];
  19. list($name,$age,$id) = $user;
  20. $stmt->bind_param('sii',$name,$age,$id);
  21. //执行
  22. $stmt->execute();
  23. //处理执行结果
  24. if($stmt->affected_rows > 0){
  25. echo '更新成功,';
  26. }else {
  27. echo '更新失败'.$stmt->error;
  28. }
  29. //关闭数据库
  30. $mysqli->close();

以下是删除操作

  1. <?php
  2. namespace mysql;
  3. use mysqli;
  4. //删除
  5. //用mysqli来进行数据库操作
  6. //链接数据库
  7. $mysqli = new mysqli('localhost','root','root','phpedu');
  8. //数据库操作
  9. //初始化返回预处理对象
  10. $stmt = $mysqli->stmt_init();
  11. // 准备SQL执行语句
  12. //$sql = 'UPDATE `staffs` SET `name`=?,`age`=? WHERE `id`=?';
  13. $sql = 'DELETE FROM `staffs` WHERE `id`>=?';
  14. //创建sql语句对象
  15. $stmt->prepare($sql);
  16. //var_dump($stmt);
  17. //绑定参数
  18. $user = [14];
  19. list($id) = $user;
  20. $stmt->bind_param('i',$id);
  21. //执行
  22. $stmt->execute();
  23. //处理执行结果
  24. if($stmt->affected_rows > 0){
  25. echo '删除成功,';
  26. }else {
  27. echo '删除失败'.$stmt->error;
  28. }
  29. //关闭数据库
  30. $mysqli->close();

以下是查询操作

  1. <?php
  2. namespace mysql;
  3. use mysqli;
  4. //查询操作
  5. //STMT查询操作
  6. //连接数据库
  7. $mysqli = new mysqli('localhost','root','root','phpedu');
  8. //var_dump($mysqli);
  9. //创建SQL语句
  10. $sql = 'SELECT `id`,`name`,`sex` FROM `staffs` ';
  11. //创建预处理对象
  12. $stmt = $mysqli->stmt_init();
  13. $stmt->prepare($sql);
  14. //执行
  15. $stmt->execute();
  16. //处理执行结果
  17. //绑定结果集
  18. $stmt->bind_result($id,$name,$sex);
  19. //遍历结果集
  20. //2,循环结果遍历
  21. while ($stmt->fetch()){
  22. if ($sex ==1 ) {
  23. echo "$id 号选手是:$name,性别男 '<br>'";
  24. }else{
  25. echo "$id 号选手是:$name, 性别女'<br>' ";
  26. }
  27. }

接口与多态实战

创建基础接口

  1. <?php
  2. namespace chapterA;
  3. //创建基础接口_接口常量
  4. interface iPARAM
  5. {
  6. const SDN = 'mysql';
  7. const HOST = 'localhost';
  8. const USERNAME = 'root';
  9. const PASSWORD = 'root';
  10. const CHARSET = 'utf8';
  11. const DBNAME = 'phpedu';
  12. }
  13. //创建接口方法——构造函数,连接数据库
  14. interface iMYSQL
  15. {
  16. public function __construct(...$link);
  17. }
  18. //进行接口继承,以后就用以下主接口
  19. interface iCURD extends iPARAM, iMYSQL
  20. {
  21. // 增
  22. public function insert(array $data);
  23. // 删
  24. public function delete(string $where);
  25. // 改
  26. public function update(array $data,string $where);
  27. // 查
  28. public function select(string $where);
  29. }

实现基础接口PDO

  1. <?php
  2. namespace chapterA;
  3. //用PDO操作数据库
  4. use PDO;
  5. //实现主接口
  6. require 'demo8.php';
  7. class PDO_mysql implements iCURD
  8. {
  9. //连接对象
  10. private $pdo = '';
  11. //连接数据库
  12. public function __construct(...$link)
  13. {
  14. list($dsn,$username,$password) = $link;
  15. $this->pdo = new PDO($dsn,$username,$password);
  16. }
  17. // 增
  18. public function insert(array $data)
  19. {
  20. $sql = "INSERT `users` SET `name`=?,`email`=?,`password`=?,`register_time`=?";
  21. $stmt = $this->pdo->prepare($sql);
  22. $stmt->execute($data);
  23. return $stmt->rowCount() === 1 ? '添加成功' : '添加失败' ;
  24. }
  25. // 删
  26. public function delete(string $where)
  27. {
  28. $sql = "DELETE FROM `users` WHERE {$where }" ;
  29. $stmt = $this->pdo->prepare($sql);
  30. $stmt->execute();
  31. return $stmt->rowCount() === 1 ? '删除成功' : '删除失败' ;
  32. }
  33. // 改
  34. public function update(array $data,string $where)
  35. {
  36. // 获取输入数据并转化为更新参数
  37. $params = '';
  38. foreach ($data as $key => $value){
  39. $params .= "`$key`= '{$value}' ,";
  40. }
  41. // 去除右边的‘,’;
  42. $params = rtrim($params, ',');
  43. // 执行
  44. $sql = "UPDATE `users` SET {$params} WHERE {$where}";
  45. $stmt = $this->pdo->prepare($sql);
  46. $stmt->execute();
  47. return $stmt->rowCount() === 1 ? '更改成功' : '更改失败' ;
  48. }
  49. // 查
  50. public function select(string $where)
  51. {
  52. $where = empty($where) ? $where : 'WHERE' . $where;
  53. $sql = "SELECT * FROM `users` {$where}";
  54. $stmt = $this->pdo->prepare($sql);
  55. $stmt->execute();
  56. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  57. }
  58. }

实现基础接口Mysqli

  1. <?php
  2. namespace chapterA;
  3. //用mysqli操作数据库
  4. use mysqli;
  5. //实现主接口
  6. require 'demo8.php';
  7. class Mysqli_mysql implements iCURD
  8. {
  9. //连接对象
  10. private $mysqli = '';
  11. //连接数据库
  12. public function __construct(...$link)
  13. {
  14. list($host,$username,$password,$dbname) = $link;
  15. $this->mysqli = new mysqli($host,$username,$password,$dbname);
  16. //设置默认字符集
  17. $this->mysqli->set_charset('utf8');
  18. }
  19. // 增
  20. public function insert(array $data)
  21. {
  22. $sql = 'INSERT `users` SET `name`=?,`email`=?,`password`=?,`register_time`=? ';
  23. $stmt = $this->mysqli->prepare($sql);
  24. list($name,$email,$password,$register_time) = $data;
  25. $stmt->bind_param('sssi',$name,$email,$password,$register_time);
  26. $stmt->execute();
  27. return $stmt->affected_rows === 1 ? '添加成功' : '添加失败' ;
  28. }
  29. // 删
  30. public function delete(string $where)
  31. {
  32. $sql = "DELETE FROM `users` WHERE {$where }" ;
  33. $stmt = $this->mysqli->prepare($sql);
  34. $stmt->execute();
  35. return $stmt->affected_rows === 1 ? '删除成功' : '删除失败' ;
  36. }
  37. // 改
  38. public function update(array $data,string $where)
  39. {
  40. $params = ' ';
  41. foreach ($data as $key=>$value){
  42. $params .= "`{$key}` = '{$value}' ,";
  43. }
  44. $params = rtrim($params, ',');
  45. $sql = "UPDATE `users` SET $params WHERE $where ";
  46. $stmt = $this->mysqli->prepare($sql);
  47. $stmt->execute();
  48. return $stmt->affected_rows === 1 ? '更改成功' : '更改失败' ;
  49. }
  50. // 查
  51. public function select( string $where)
  52. {
  53. $where = empty($where) ? $where : 'WHERE' . $where;
  54. $sql = "SELECT * FROM `users` {$where}";
  55. $stmt = $this->mysqli->prepare($sql);
  56. $stmt->execute();
  57. return $stmt->get_result();
  58. }
  59. }

创建通用类

  1. <?php
  2. namespace chapterA;
  3. use chapterA\iCURD;
  4. //通用类,数据库操作,面向接口实现多态
  5. class DB
  6. {
  7. // 增
  8. public function insert(iCURD $db, array $data){
  9. return $db->insert($data);
  10. }
  11. // 删
  12. public function delete(iCURD $db,string $where){
  13. return $db->delete($where);
  14. }
  15. // 改
  16. public function update(iCURD $db,array $data,string $where){
  17. return $db->update($data,$where);
  18. }
  19. // 查
  20. public function select(iCURD $db,string $where){
  21. return $db->select($where);
  22. }
  23. }

进行数据库操作

  1. <?php
  2. //使用PDO操作数据库:接口实现
  3. namespace chapterA;
  4. use PDO;
  5. //加载PDO接口
  6. require 'demo9.php';
  7. //加载通用类
  8. require 'demo11.php';
  9. //创建DSN
  10. $dsn = iPARAM::SDN.':host='.iPARAM::HOST.';dbname='.iPARAM::DBNAME.';';
  11. //连接数据库
  12. $link = new PDO_mysql($dsn,PDO_mysql::USERNAME,PDO_mysql::PASSWORD);
  13. //var_dump($link);
  14. //die();
  15. //测试增加操作
  16. //$pdop = new DB();
  17. //echo $pdop->insert($link,['小江','jiang@php.cn',sha1('123456'),time()]);
  18. //测试删除操作
  19. //$pdop = new DB();
  20. //echo $pdop->delete($link,'`id`=9');
  21. //测试更新数据
  22. //$pdop = new DB();
  23. //echo $pdop->update($link,['name'=>'小李','email'=>'jiang@php.cn'],'id=7');
  24. //测试查询操作
  25. $pdop = new DB();
  26. foreach ($pdop->select($link,'`id`>2') as $user){
  27. echo "{$user['id']} -- {$user['name']} -- {$user['email']} <br>";
  28. }

手写mysqli_result相关类用途

上传不起,一直显示未发现,,,

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:mysqli做为知识扩展, 还有要掌握的, 不要小看, 还有许多项目在使用
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