Blogger Information
Blog 24
fans 4
comment 0
visits 20136
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
2月11日 学号:478291 MySQLi常用CURD操作、通过接口实现多态
Lin__
Original
625 people have browsed it

MySQLi常用CURD操作

  • 连接数据库:mysqli(主机名,用户名,密码,数据库名)
  • 数据库操作:

(1)stmt_init(),返回预处理对象

(2)prepare(SQL语句),创建语句对象

(3)bind_param(参数类型,参数列表),绑定参数

(4)execite(),执行

(5)affected_row(),返回受影响的记录数

(6)insert_id,新增的记录的ID

(7)error,错误信息,字符串

(8)fetch(),获取一条记录,stmt方法

(9)get_result(),返回结果集对象实例,结果集方式方法

(10)fetch_array(),一条记录结果,为索引+关联的一维数组,结果集方式方法

(11)fetch_row(),一条记录结果,为索引一维数组,结果集方式方法

(12)fetch_assoc(),一条记录结果,为关联一维数组,结果集方式方法

(13)fetch_all,一次性获取所有记录,索引二维数组,若要改成关联数组,需添加参数MYSQL_ASSOC,结果集方式方法

(14)data_seek(),数据库指针复位

(15)free_result(),释放结果集

  • 关闭数据库连接:close()

使用mysqli操作数据库

数据表

新增操作

  1. <?php
  2. //连接数据库
  3. $conn=new mysqli('localhost','root','root','test_db');
  4. //准备sql语句
  5. $sql="insert `user` set `name`=? , `age` = ?";
  6. //预处理方式
  7. $stmt=$conn->stmt_init();
  8. //创建语句对象
  9. $stmt->prepare($sql);
  10. //绑定参数
  11. $name='Jane';
  12. $age=35;
  13. $stmt->bind_param('si',$name,$age);
  14. //执行
  15. $stmt->execute();
  16. //处理执行结果
  17. if($stmt->affected_rows){
  18. echo "新增成功,新增的ID是{$stmt->insert_id}";
  19. }else{
  20. echo "新增失败,错误原因{$stmt->error}";
  21. }
  22. //关闭数据库连接
  23. $stmt->close();



修改操作

  1. <?php
  2. //连接数据库
  3. $conn=new mysqli('localhost','root','root','test_db');
  4. //准备sql语句
  5. $sql="update `user` set `name`=? where id=?";
  6. //预处理方式
  7. $stmt=$conn->stmt_init();
  8. //创建语句对象
  9. $stmt->prepare($sql);
  10. //绑定参数
  11. $name='Maria';
  12. $id=5;
  13. $stmt->bind_param('si',$name,$id);
  14. //执行
  15. $stmt->execute();
  16. //处理执行结果
  17. if($stmt->affected_rows){
  18. echo "修改成功";
  19. }else{
  20. echo "修改失败,错误原因{$stmt->error}";
  21. }
  22. //关闭数据库连接
  23. $stmt->close();



删除操作

  1. <?php
  2. //连接数据库
  3. $conn=new mysqli('localhost','root','root','test_db');
  4. //准备sql语句
  5. $sql="delete from `user` where id=?";
  6. //预处理方式
  7. $stmt=$conn->stmt_init();
  8. //创建语句对象
  9. $stmt->prepare($sql);
  10. //绑定参数
  11. $id=5;
  12. $stmt->bind_param('i',$id);
  13. //执行
  14. $stmt->execute();
  15. //处理执行结果
  16. if($stmt->affected_rows){
  17. echo "删除成功";
  18. }else{
  19. echo "删除失败,错误原因{$stmt->error}";
  20. }
  21. //关闭数据库连接
  22. $stmt->close();



查询操作

  1. <?php
  2. //连接数据库
  3. $conn=new mysqli('localhost','root','root','test_db');
  4. //准备sql语句
  5. $sql="select * from `user`";
  6. //预处理方式
  7. $stmt=$conn->stmt_init();
  8. //创建语句对象
  9. $stmt->prepare($sql);
  10. //绑定参数
  11. $stmt->bind_result($id,$name,$age);
  12. //执行
  13. $stmt->execute();
  14. //处理执行结果
  15. while($result=$stmt->fetch()){
  16. echo "{$name}的年龄是{$age}<br/>";
  17. }
  18. //释放结果集
  19. $stmt->free_result();
  20. //关闭数据库连接
  21. $stmt->close();

通过接口实现多态

数据操作接口

  1. <?php
  2. namespace db;
  3. //定义常用参数接口
  4. interface IParam{
  5. const HOST='localhost';//主机名称
  6. const USERNAME='root';//用户名
  7. const PASSWORD='root';//密码
  8. const PORT='3306';//端口号
  9. const TYPE='mysql';//数据库类型
  10. const CHARSET='utf8';//编码集
  11. const DBNAME='test_db';//数据库名称
  12. }
  13. //定义构造方法接口,用于连接数据库
  14. interface IStruction{
  15. public function __construct(...$paramArr);
  16. }
  17. //定义常用数据库操作方法接口
  18. interface IMethod extends IParam,IStruction{
  19. public function insert(string $tableName,array $data);//添加
  20. public function select(string $tableName,string $field,string $where);//查询
  21. public function update(string $tableName,array $data,string $where);//修改
  22. public function delete(string $tableName,string $where);//删除
  23. }

pdo数据库操作类

  1. <?php
  2. namespace db;
  3. require_once 'interface.php';
  4. class Pdo_db implements IMethod{
  5. private $pdo;
  6. public function __construct(...$paramArr){
  7. list($dsn, $username, $password) = $paramArr;
  8. $this->pdo=new \PDO($dsn, $username, $password);
  9. }
  10. public function insert(string $tableName,array $data){
  11. $param='';
  12. foreach($data as $k=>$v){
  13. if(gettype($v)=='integer'){
  14. $param.=$k."= {$v},";
  15. }else{
  16. $param.=$k."= '{$v}',";
  17. }
  18. }
  19. $param=rtrim($param,',');
  20. $sql="insert `{$tableName}` set {$param} ;";
  21. $stmt=$this->pdo->prepare($sql);
  22. $stmt->execute();
  23. if($stmt->rowCount()){
  24. echo "新增成功,新增的记录ID为{$pdo->lastInsertId()}";
  25. }else{
  26. echo "新增失败,错误为:{$pdo->errorInfo()}";
  27. }
  28. }
  29. public function select(string $tableName,string $field,string $where){
  30. $sql="select {$field} from `{$tableName}` where {$where}";
  31. $stmt=$this->pdo->prepare($sql);
  32. $stmt->execute();
  33. while($result=$stmt->fetch(\PDO::FETCH_ASSOC)){
  34. var_dump($result);
  35. echo '<br/>';
  36. }
  37. }
  38. public function update(string $tableName,array $data,string $where){
  39. $param='';
  40. foreach($data as $k=>$v){
  41. if(gettype($v)=='integer'){
  42. $param.=$k."= {$v},";
  43. }else{
  44. $param.=$k."= '{$v}',";
  45. }
  46. }
  47. $param=rtrim($param,',');
  48. $sql="update `{$tableName}` set {$param} where {$where}";
  49. $stmt=$this->pdo->prepare($sql);
  50. $stmt->execute();
  51. if($stmt->rowCount()){
  52. echo "修改成功";
  53. }else{
  54. echo "修改失败,错误为:{$pdo->errorInfo()}";
  55. }
  56. }
  57. public function delete(string $tableName,string $where){
  58. $sql="delete from `{$tableName}` where {$where}";
  59. $stmt=$this->pdo->prepare($sql);
  60. $stmt->execute();
  61. if($stmt->rowCount()){
  62. echo "删除成功";
  63. }else{
  64. echo "删除失败,错误为:{$pdo->errorInfo()}";
  65. }
  66. }
  67. }

mysqli数据库操作类

  1. <?php
  2. namespace db;
  3. require_once 'interface.php';
  4. class Mysqli_db implements IMethod{
  5. private $conn;
  6. public function __construct(...$paramArr){
  7. $this->conn=new mysqli($paramArr['host'],$paramArr['username'],$paramArr['password'],$paramArr['dbname']);
  8. }
  9. public function insert(string $tableName,array $data){
  10. $param='';
  11. foreach($data as $k=>$v){
  12. if(gettype($v)=='integer'){
  13. $param.=$k."= {$v},";
  14. }else{
  15. $param.=$k."= '{$v}',";
  16. }
  17. }
  18. $param=rtrim($param,',');
  19. $sql="insert `{$tableName}` set {$param} ;";
  20. $stmt=$this->conn->stmt_init();
  21. $stmt->prepare($sql);
  22. $stmt->execute();
  23. if($stmt->affected_rows){
  24. echo "新增成功,新增的ID是{$stmt->insert_id}";
  25. }else{
  26. echo "新增失败,错误原因{$stmt->error}";
  27. }
  28. $stmt->close();
  29. }
  30. public function select(string $tableName,string $field,string $where){
  31. $sql="select {$field} from `{$tableName}` where {$where}";
  32. $stmt=$this->conn->stmt_init();
  33. $stmt->prepare($sql);
  34. $stmt->execute();
  35. while($result=$stmt->fetch_assoc()){
  36. echo '<pre>'.print_r($result,1).'</pre>';
  37. }
  38. $stmt->free_result();
  39. $stmt->close();
  40. }
  41. public function update(string $tableName,array $data,string $where){
  42. $param='';
  43. foreach($data as $k=>$v){
  44. if(gettype($v)=='integer'){
  45. $param.=$k."= {$v},";
  46. }else{
  47. $param.=$k."= '{$v}',";
  48. }
  49. }
  50. $param=rtrim($param,',');
  51. $sql="update `{$tableName}` set {$param} where {$where}";
  52. $stmt=$this->conn->stmt_init();
  53. $stmt->prepare($sql);
  54. $stmt->execute();
  55. if($stmt->affected_rows){
  56. echo "修改成功";
  57. }else{
  58. echo "修改失败,错误为:{$pdo->errorInfo()}";
  59. }
  60. $stmt->close();
  61. }
  62. public function delete(string $tableName,string $where){
  63. $sql="delete from `{$tableName}` where {$where}";
  64. $stmt=$this->conn->stmt_init();
  65. $stmt->prepare($sql);
  66. $stmt->execute();
  67. if($stmt->affected_rows){
  68. echo "删除成功";
  69. }else{
  70. echo "删除失败,错误为:{$pdo->errorInfo()}";
  71. }
  72. $stmt->close();
  73. }
  74. }

测试

  1. <?php
  2. namespace db;
  3. require_once 'pdo_db.php';
  4. require_once 'mysqli_db.php';
  5. $dsn=IParam::TYPE . ':host='.IParam::HOST . ';dbname=' . IParam::DBNAME . ';charset='.IParam::CHARSET;
  6. $link=new Pdo_db($dsn, IParam::USERNAME, IParam::PASSWORD);
  7. $link->select('user','*','id=1');

运行结果

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