Blogger Information
Blog 24
fans 0
comment 0
visits 18691
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysqli操作数据库
昔年
Original
824 people have browsed it

mysqli操作数据库

1.创建数据表

  1. DROP TABLE IF EXISTS `goods`;
  2. create table `goods` (
  3. `id` int unsigned not null auto_increment,
  4. `name` varchar(20) COLLATE utf8mb4_unicode_ci not null,
  5. `stock` int unsigned not null,
  6. `price` float,
  7. PRIMARY KEY(`id`)
  8. )ENGINE=InnoDB auto_increment=1 default charset=utf8mb4 COLLATE=utf8mb4_unicode_ci;

2.连接数据库

2.1 数据库配置文件

  1. <?php
  2. namespace mysqli_eud;
  3. return [
  4. 'type' => $type ?? 'mysql',
  5. 'host' => $host ?? 'localhost',
  6. 'charset' => $charset ?? 'utf8',
  7. 'dbname' => $dbname ?? 'phpedu',
  8. 'username' => $username ?? 'root',
  9. 'password' => $password ?? 'root'
  10. ];

2.2 使用mysqli连接数据库

  1. <?php
  2. namespace mysqli_edu;
  3. use mysqli;
  4. //1、引入数据库配置文件
  5. $config = require __DIR__ . './config/database.php';
  6. // 2. mysqli 连接四大参数
  7. // 2.1 数据库的主机名: host
  8. // 2.1 用户名: username
  9. // 2.3 用户密码: password
  10. // 2.4 默认的数据库 :dbname
  11. /**
  12. * @var string $host
  13. * @var string $username
  14. * @var string $password
  15. * @var string $dbanme
  16. * @var string $charset
  17. */
  18. //将关联数组键值对转为变量的名值对
  19. extract($config);
  20. //2.链接数据库
  21. $mysqli = new mysqli($host, $uername, $password, $dbname);
  22. //3.判断是否链接成功
  23. if ($mysqli->connect_errno) echo $mysqli->connect_error;
  24. //4.设置客户端编码字符集
  25. $mysqli->set_charset($charset);
  26. // var_dump($mysqli);

3.使用mysqli实现对数据库的读操作

  1. <?php
  2. namespace select;
  3. use mysqli;
  4. use mysqli_result;
  5. //1.连接数据库
  6. require 'connect.php';
  7. //2.操作数据库
  8. $sql = "select * from `goods`";
  9. $mysqli_result = $mysqli->query($sql);
  10. //指针复位
  11. $mysqli_result->data_seek(0);
  12. // var_dump($mysqli_result);
  13. if ($mysqli_result && $mysqli_result->num_rows > 0) {
  14. $goods = $mysqli_result->fetch_all();
  15. foreach ($goods as $good) {
  16. vprintf('<li>编号:%s , 名称: %s , 库存: %s, 价格: %s</li>', $good);
  17. }
  18. }

4.使用mysqli实现对数据的增操作

  1. <?php
  2. namespace insert;
  3. use mysqli;
  4. //1.链接数据库
  5. require 'connect.php';
  6. //2.数据库操作
  7. $data = ['啤酒', '49', '5'];
  8. array_walk($data, function (&$item, $key, $length) {
  9. if ($key < $length - 1) $item = "'$item'";
  10. }, count($data));
  11. $str = implode(',', $data);
  12. $sql = "insert `goods` (`name`, `stock`, `price`) values ($str)";
  13. // echo $sql;
  14. // die;
  15. if ($mysqli->query($sql)) {
  16. echo '成功添加了 ' . $mysqli->affected_rows . ' 条记录, 新增记录主键ID: ' . $mysqli->insert_id;
  17. } else {
  18. die('添加失败' . $mysqli->errno . ' : ' . $mysqli->error);
  19. }
  20. //3.关闭连接
  21. $mysqli->close();

4.使用mysqli实现对数据的改操作

  1. <?php
  2. namespace update;
  3. use mysqli;
  4. require 'connect.php';
  5. $arr = ['name' => '酸奶', 'stock' => 56, 'price' => 8];
  6. array_walk($arr, function (&$item, $key) {
  7. $item = "`$key` = '$item'";
  8. });
  9. $data = implode(', ', $arr);
  10. $sql = "update `goods` set $data where `id` = 6";
  11. // var_dump($sql);
  12. // die;
  13. if ($mysqli->query($sql)) {
  14. if ($mysqli->affected_rows > 0) {
  15. echo '成功更新了 ' . $mysqli->affected_rows . ' 条记录';
  16. } else {
  17. echo '没有更新任何记录';
  18. }
  19. } else {
  20. die('更新失败' . $mysqli->errno . ' : ' . $mysqli->error);
  21. }
  22. // 3. 关闭连接
  23. // 释放结果集
  24. $mysqli->close();

6.使用mysqli实现对数据的删操作

  1. <?php
  2. namespace delete;
  3. use mysqli;
  4. //1.连接数据库
  5. require "connect.php";
  6. //2.数据库操作
  7. $sql = "delete from `goods` where `id`=" . $_GET['id'];
  8. if ($mysqli->query($sql)) {
  9. if ($mysqli->affected_rows > 0) {
  10. echo '成功删除了id=' . $_GET['id'] . '的记录';
  11. }
  12. } else {
  13. die('删除失败' . $mysqli->errno . ':' . $mysqli->error);
  14. }

总结:mysqli操作数据库和pdo操作数据库的步骤几乎差不多都是分成三步,连接数据库、数据库操作、关闭连接(可选)。重点是在第二部数据库的操作部分,特别是在增和改时,需要用到数组函数array_walk()对数据进行处理。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:拼装sql语句的方式很多, 我用array_walk()只是其中一种方式, 或者你还有更好的办法,想想看
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!