Blogger Information
Blog 17
fans 1
comment 0
visits 20047
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP:mysqli演示示例
大A
Original
798 people have browsed it

案例介绍

建立一个mysqli查询类,使用内部方法完成增删改查数据库.

数据表

源码

  1. <?php
  2. $config = [
  3. 'host' => 'localhost',
  4. 'username' => 'root',
  5. 'password' => 'root',
  6. 'dbname' => 'phpedu'
  7. ];
  8. //创建一个mysqli查询类
  9. class db
  10. {
  11. private $mysqli;
  12. public function __construct($content)
  13. {
  14. extract($content);
  15. $this->mysqli = new mysqli($host, $username, $password, $dbname);
  16. return $this;
  17. }
  18. //查询商品价格
  19. public function select($name, &$price = null)
  20. {
  21. $sql = "SELECT `price` FROM products WHERE `name`='$name'";
  22. $sql_obj = $this->mysqli->query($sql);
  23. $price = $sql_obj->fetch_assoc()['price'];
  24. if ($price === null) :
  25. return false;
  26. else :
  27. return true;
  28. endif;
  29. }
  30. //增加一个商品
  31. public function insert($name, $price, &$id = null)
  32. {
  33. $sql = "INSERT `products` SET `name`='$name',`price`=$price";
  34. $sql_obj = $this->mysqli->query($sql);
  35. $id = $this->mysqli->insert_id;
  36. if ($id === 0) :
  37. return false;
  38. else :
  39. return true;
  40. endif;
  41. }
  42. //修改一个商品的价格
  43. public function update($name, $price, &$result)
  44. {
  45. $sql = "UPDATE `products` SET `price`=$price WHERE `name`='$name'";
  46. $this->mysqli->query($sql);
  47. $result = $this->mysqli->affected_rows;
  48. if ($result === 0) :
  49. return false;
  50. else :
  51. $result = $price;
  52. return true;
  53. endif;
  54. }
  55. //删除一个商品
  56. public function delete($name)
  57. {
  58. $sql = "DELETE FROM `products` WHERE `name`='$name'";
  59. $this->mysqli->query($sql);
  60. $result = $this->mysqli->affected_rows;
  61. if ($result === 0) :
  62. return false;
  63. else :
  64. return true;
  65. endif;
  66. }
  67. }
  68. //建立查询对象
  69. $db = new db($config);
  70. //查询商品价格
  71. if ($db->select('真维斯', $price)) :
  72. echo '真维斯:' . $price . '<hr>';
  73. else :
  74. echo '查询的商品信息不存在<hr>';
  75. endif;
  76. // //录入商品
  77. if ($db->insert('李宁', '525', $id)) :
  78. echo '录入商品成功!商品ID为:' . $id . '<hr>';
  79. else :
  80. echo '录入失败!<hr>';
  81. endif;
  82. //修改商品价格
  83. $name = '真维斯';
  84. if ($db->update($name, '356', $price)) :
  85. echo '已将' . $name . '的价格修改为' . $price . '<hr>';
  86. else :
  87. echo '修改商品失败<hr>';
  88. endif;
  89. // //删除商品
  90. if ($db->delete('联想')) :
  91. echo '删除成功<hr>';
  92. else :
  93. echo '删除失败<hr>';
  94. endif;

运行结果

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!