Blogger Information
Blog 36
fans 1
comment 0
visits 29630
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php与mysqli查询
Jason
Original
658 people have browsed it

php之mysqli查询

任何增删改查都是基于数据表进行的,没有数据,就无法进行,现在我们来新建一张表,对表进行操作

如下

mysqli的基本操作

示例:

  1. // 增加操作
  2. require 'connect.php';
  3. $arr = ['橘子','夏季',999];
  4. array_walk($arr,function(&$item,$key,$length) {
  5. if($key < $length-1) $item = "'$item'";
  6. },count($arr));
  7. $data = implode(',',$arr);
  8. $sql = "INSERT `goods`(`name`,`category`,`price`)VALUES($data)";
  9. if($mysqli->query($sql)){
  10. if($mysqli->affected_rows>0) {
  11. echo '成功添加了',$mysqli->affected_rows.'条记录,新增
  12. 记录ID:'.$mysqli->insert_id;
  13. }else {
  14. echo '没有添加新记录';
  15. }
  16. }else {
  17. die('添加失败'.$mysqli->error.':'.$mysqli->error);
  18. }
  19. $mysqli->close();
  20. echo '<br>';
  21. // 删除操作
  22. require 'connect.php';
  23. $sql = "DELETE FROM `goods` where `id` = ".$_GET['id'];
  24. if($mysqli->query($sql)){
  25. if($mysqli->affected_rows>0) {
  26. echo '成功删除了 id='.$_GET['id'].'的记录';
  27. }else{
  28. echo '没有删除记录';
  29. }
  30. }else{
  31. die('删除失败'.$mysqli->error.':'.$mysqli->error);
  32. }
  33. $mysqli->close();
  34. echo '<br>';
  35. // 修改
  36. require 'connect.php';
  37. $arr = ['name'=> '梨子','price' => 19];
  38. array_walk($arr,function(&$item,$key){
  39. $item = "`$key` = '$item'";
  40. });
  41. $data = implode(',',$arr);
  42. $sql = "UPDATE `goods` SET " . $data . " WHERE `id` = '5'";
  43. if ($mysqli->query($sql)) {
  44. if($mysqli->affected_rows>0) {
  45. echo '成功更新了'.$mysqli->affected_rows.'条记录';
  46. }else{
  47. echo '没有更新任何记录';
  48. }
  49. }else {
  50. die('更新失败'.$mysqli->error.':'.$mysqli->error);
  51. }
  52. $mysqli->close();
  53. echo '<br>';
  54. // 查询操作
  55. require 'connect.php';
  56. $sql = "SELECT `id`,`name`,`price` FROM `goods` WHERE `id` > 2";
  57. $mysqli_result = $mysqli->query($sql);
  58. if($mysqli_result && $mysqli_result -> num_rows > 0) {
  59. $staffs = $mysqli_result->fetch_all();
  60. foreach($staffs as $staff){
  61. vprintf('<li>id:%s,名字:%s,价格:%s</li>',$staff);
  62. }
  63. }else {
  64. echo '查询失败';
  65. }
  66. $mysqli_result->free_result();
  67. $mysqli->close();
  68. echo '<br>';

输出:

  1. 成功添加了1条记录,新增 记录ID:8
  2. 成功删除了 id=4的记录
  3. 成功更新了1条记录
  4. id:3,名字:离子,价格:19
  5. id:5,名字:梨子,价格:19
  6. id:6,名字:栗子,价格:3333
  7. id:7,名字:葡萄,价格:4444
  8. id:8,名字:橘子,价格:999

总结

有了上次做PDO的经历,这次做MYSQL顺手多了,基本就是显示结果的时候改了一下,成功拿下,当然也有一些小问题,在编写SQL语句时,键名应该用`来阔上,php才能解析,后面调试的过程就把语句先打印出来,再放到adminer中执行一下,发现错了,赶紧查找原因,才找到是符号错了,此类的问题还有。只能说越挫越勇,遇到困难可能会有点难,但是找方法,问题总能够解决。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!