Blogger Information
Blog 40
fans 0
comment 1
visits 39783
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用mysqli类的对象去连接数据库
Dong.
Original
857 people have browsed it

使用mysqli类的对象去连接数据库,然后获取到mysqli_result对象,使用mysqli _result对象中的成员方法或者属性将获得的结果集中的数据(二维数组)遍历出来

  1. <?php
  2. $mysqlilink = [
  3. 'localhost' => '127.0.0.1', // 服务器地址
  4. 'database' => 'admin', // 数据库名
  5. 'username' => 'admin', // 用户名
  6. 'password' => 'huicheng123', // 密码
  7. 'hostport' => '3306', // 端口
  8. ];
  9. $mysqli = new mysqli($mysqlilink['localhost'],$mysqlilink['username'],$mysqlilink['password'],$mysqlilink['database'],$mysqlilink['hostport']);
  10. //检查连接是否成功
  11. if($mysqli->connect_error)
  12. {
  13. //如果没有错误,会返回一个null值
  14. die("连接失败,请重试:" . $mysqli->connect_error);//打印错误信息
  15. }
  16. //设置默认的客户端字符集
  17. $myaqli->set_charset('utf-8');
  18. // sql插入语句
  19. $sql = "INSERT INTO users(`name`, `email`, `password`,`time`) VALUES ('小明', 'xiaomin@php.cn','123456','15133383837');";
  20. $res = $mysqli->query($sql);//曾删
  21. // $mysqli->query()只能执行单条查询
  22. // sql更新语句
  23. $sql = "UPDATE `users` SET `age`=21 WHERE `id`=12";
  24. $res = $mysqli->query($sql);
  25. // sql删除语句
  26. $sql = "DELETE FROM `users` WHERE `name` = '灭绝师太' ";
  27. $res = $mysqli->query($sql);
  28. // sql查询语句(查询所有字段)
  29. $sql = "SELECT * FROM `users`";
  30. $res = $mysqli->query($sql);
  31. // 使用mysql_result类的fetch_all方法,以数组的方式返回结果集;
  32. $res = $res->fetch_all(MYSQLI_ASSOC);
  33. // 遍历结果集
  34. foreach($res as $key => $vulue)
  35. {
  36. echo "----{$vulue['id']}----{$vulue['name']}----{$vulue['email']}----{$vulue['time']}----
  37. ";
  38. }

总结

  • 使用mysqli类的对象去连接数据库,然后获取到mysqli_result对象,使用mysqli _result对象中的成员方法或者属性将获得的结果集中的数据(二维数组)遍历出来
  • 理解了增查改删的操作方式
  • 对于两个类的方法有了一定的理解
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