批改状态:合格
老师批语:其实mysqli操作数据库, 步骤 更加清晰, 是不是感觉到了
表名:goods
| 列 | 类型 | 注释 |
|---|---|---|
| id | int(11) 自动增量 | 主键 |
| title | varchar(20) | 名称 |
| price | decimal(10,2) | 价格 |
| status | int(2) [1] | 状态 |
| create_time | int(10) | 创建时间 |
<?php// 查询单条记录// 连接数据库require 'connect.php';// 2. 操作数据库$sql = "SELECT `id`, `title`,`price` FROM `goods` WHERE `price` > 3000";$mysqli_res = $mysqli->query($sql);// 指针复位$mysqli_res->data_seek(0);if ($mysqli_res && $mysqli_res->num_rows > 0 ) {while ($goods = $mysqli_res->fetch_assoc()) {printf('<pre>%s</pre>', print_r($goods, true));}} else {echo '查询失败或没有查询到满足条件的商品';}// 3. 关闭连接 释放结果集$mysqli_res->free_result();$mysqli->close();
Array
(
[id] => 2
[title] => 苹果手机
[price] => 6999.00
)
Array
(
[id] => 4
[title] => 联想电脑
[price] => 4888.00
)
Array
(
[id] => 5
[title] => 惠普电脑
[price] => 4666.00
)
<?php// 查询多条记录// 连接数据库require 'connect.php';// 2. 操作数据库$sql = "SELECT `id`, `title`,`price` FROM `goods` WHERE `price` > 3000";$mysqli_res = $mysqli->query($sql);if ($mysqli_res && $mysqli_res->num_rows > 0 ) {$goods = $mysqli_res->fetch_all();printf('<table>');printf('<tr><td>编号</td><td>商品</td><td> 价格</td></tr>');foreach ($goods as $title) {// vprintf() 与 printf()功能一样, 区别 就是参数是数组vprintf('<tr><td>%s </td><td> %s</td><td> %s</td></tr>', $title);}printf('</table>');} else {echo '查询失败或没有查询到满足条件的商品';}// 3. 关闭连接 释放结果集$mysqli_res->free_result();$mysqli->close();
| 编号 | 商品 | 价格 |
|---|---|---|
| 2 | 苹果手机 | 6999.00 |
| 4 | 联想电脑 | 4888.00 |
| 5 | 惠普电脑 | 4666.00 |
<?php// 新增操作// 1. 连接数据库require 'connect.php';// 2. 操作数据库// 前端表单->json格式的字符串, php将这种json字符串转为数组/对象$arr = ['苹果电脑', 9999, '1', 1588123456];array_walk($arr, function(&$item, $key, $length) {if ($key < $length-1 ) $item = "'$item'";}, count($arr));$data = implode(', ', $arr);$sql = "INSERT `goods` (`title`,`price`,`status`,`create_time`) VALUES ($data)";if ($mysqli->query($sql)) {if ($mysqli->affected_rows > 0) {echo '成功添加了 ' . $mysqli->affected_rows . ' 条记录, 新增记录主键ID: ' . $mysqli->insert_id;} else {echo '没有添加新记录';}} else {die('添加失败'. $mysqli->errno . ' : ' . $mysqli->error);}// 3. 关闭连接// 释放结果集$mysqli->close();
成功添加了 1 条记录, 新增记录主键ID: 6
<?php// 删除操作// 1. 连接数据库require 'connect.php';// 2. 操作数据库$sql = "DELETE FROM `goods` WHERE `id` =" . $_GET['id'];if ($mysqli->query($sql)) {if ($mysqli->affected_rows > 0) {echo '成功删除了 id=' .$_GET['id'] . ' 的记录';} else {echo '没有删除任何记录';}} else {die('删除失败'. $mysqli->errno . ' : ' . $mysqli->error);}// 3. 关闭连接$mysqli->close();
成功删除了 id=6 的记录
<?php// 更新操作// 1. 连接数据库require 'connect.php';// 2. 操作数据库// 前端表单->json格式的字符串, php将这种json字符串转为数组/对象$arr = ['title'=>'华为服务器', 'price'=>'39999'];array_walk($arr, function(&$item, $key) {$item = "`$key` = '$item'";});$data = implode(', ', $arr);$sql = "UPDATE `goods` SET " . $data . " WHERE `id` = 5";if ($mysqli->query($sql)) {if ($mysqli->affected_rows > 0) {echo '成功更新了 ' . $mysqli->affected_rows . ' 条记录';} else {echo '没有更新任何记录';}} else {die('更新失败'. $mysqli->errno . ' : ' . $mysqli->error);}// 3. 关闭连接// 释放结果集$mysqli->close();
成功更新了 1 条记录
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号