Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
//第一种插入语句
INSERT INTO `user`VALUES(NULL,'小红',18345647559,'2021-10-19 16:08:53',0)
//第二种插入语句
INSERT INTO `user`('id')VALUES(NULL)
//第三种插入语句
INSERT INTO `user`SET `name` = '小明' ,
// 直接查询表里的全部数据
SELECT * FROM `user`
// 查询id 大于10 的
SELECT * FROM `user` WHERE `id` > 10
// 查询小于6的 和 大于18的
SELECT * FROM `user` WHERE `id` < 6 OR `id` >10
// 查询name 为空的数据
SELECT * FROM `user` WHERE `name` IS NULL
// 查询name 不为空的数据
SELECT * FROM `user` WHERE `name` IS NOT NULL
// 获取 >=10 AND <=20 的值
SELECT * FROM `user` WHERE `id` BETWEEN 10 AND 20
// 获取除去 >=10 AND <=20 的值
SELECT * FROM `user` WHERE `id` NOT BETWEEN 10 AND 20
// 查询 xiao 后面还有五个字符的数据
SELECT * FROM `user` WHERE `account` LIKE 'xiao_____'
// 查询所有带有a的数据
SELECT * FROM `user` WHERE `account` LIKE '%a%'
*号 会把所有返回值都返回
例:
SELECT`name`,`phone`,`age`FROM `user`WHERE`account`LIKE'%o%'
// 分页,当前页只看到查询内容的10条
SELECT `name`,`phone`,`age` FROM `user` WHERE `account` LIKE '%o%' LIMIT 10
// 从小到大
SELECT `id`,`name`,`phone`,`age` FROM `user` WHERE `account` LIKE '%o%' ORDER BY `id` LIMIT 0,10
// 从大到小
SELECT `id`,`name`,`phone`,`age` FROM `user` WHERE `account` LIKE '%o%' ORDER BY DESC `id` LIMIT 0,10
WHERE条件,也可以用在,修改和删除里,但是不建议在mysql语句来修改和删除
例
// 把第4条数据修改了
UPDATE `user`SET `name` = '小李',`phone` = '12345678912'WHERE `id` = 4
// 把 id =5 的数据删除
DELETE FROM `user` WHERE `id` = 5
$pdo = new PDO(数据源,用户名,密码);
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php', 'root', 'root');
var_dump($pdo);
$pre = $pdo->prepare('SELECT * FROM `user`');
var_dump($pre);
$exe = $pre->execute();
$data = $pre->fetchAll();
// 当数据库mysql没有开启是就会报错
try {
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php', 'root', 'root');
} catch (PDOException $e) {
// 抛出错误,错误是你可要定义的
echo '数据库连接失败' . $e->getMessage();
}
$pre = $pdo->prepare('SELECT * FROM `user`');
$exe = $pre->execute();
$data = $pre->fetch();
print_r($data);
$pre = $pdo->prepare("INSERT INTO `user` VALUES (null, 'xiaoming', '123456', '小明', 18, '13843818908', 0, 0, 1)");
$exe = $pre->execute();
print_r($exe);
pdo 只要给个占位符 : ,在下面的代码中,传值给占位符
例
$sql = 'INSERT INTO `user` SET `account`=:account,`password`=:password,`name`=:name,`phone`=:phone';
$pre = $pdo->prepare($sql);
// 在执行之前进行传值
bindParam 参数绑定,第三个参数:
- 占位符名
- 要给值的变量,绑定上,现在可以没有值
- 常量,pdo预定义常量,可以设置这个值的类型, 访问方式: PDO:: ,是类的常量访问方式
PARAM_STR 字符串
PARAM_INT 整数
例
// 增加数据,并获取增加的id值
$pre->bindParam('account', $account, PDO::PARAM_STR);
$pre->bindParam('password', $password, PDO::PARAM_STR);
$pre->bindParam('name', $name, PDO::PARAM_STR);
$pre->bindParam('phone', $phone, PDO::PARAM_INT);
$account = 'xiaozhao';
$password = md5('123456');
$name = '小赵';
$phone = 18715745675;
$exe = $pre->execute();
if(!$ese){
print_r($pre->errorInfo());
}else{
// rowCount 获取是否成功,影响数量
echo $pre->rowCount();
echo '<hr>';
// lastInsertId 获取这次自增的ID 类,是用连接的类,是$pdo
echo $pdo->lastInsertId();
}
? 占位,数组传值,并且不需要绑定数据,直接在 execute 里传数组
例
// ? 占位符
// 添加一条数据
$sql = 'INSERT INTO `user` SET `account`=?,`password`=?,`name`=?,`phone`=?';
$pre = $pdo->prepare($sql);
$exe = $pre->execute([
'xiaoli',
md5(123456),
'小李',
13345753547
]);
if (!$exe) {
print_r($pro->errorInfo());
} else {
echo $pre->rowCount();
echo '<hr>';
echo $pdo->lastInsertId();
}
// : 明文占位符
$sql = 'INSERT INTO `user` SET `account`=:account,`password`=:password,`name`=:name,`phone`=:phone';
$pre = $pdo->prepare($sql);
$exe = $pre->execute([
':account' => 'xiaolong',
':password' => md5(123456),
':name' => '小龙',
':phone' => 18654215366
]);
if (!$exe) {
print_r($pro->errorInfo());
} else {
echo $pre->rowCount();
echo '<hr>';
echo $pdo->lastInsertId();
}
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for shop_yh
-- ----------------------------
DROP TABLE IF EXISTS `shop_yh`;
CREATE TABLE `shop_yh` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`account` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`password` char(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`phone` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`gender` char(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for shop_sp
-- ----------------------------
DROP TABLE IF EXISTS `shop_sp`;
CREATE TABLE `shop_sp` (
`id` int(10) NOT NULL,
`spmc` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
`spxx` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
`spbh` char(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
`spjg` decimal(10, 0) NULL DEFAULT NULL,
`sccj` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for shop_ddb
-- ----------------------------
DROP TABLE IF EXISTS `shop_ddb`;
CREATE TABLE `shop_ddb` (
`id` int(10) NOT NULL,
`ddbh` char(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
`yhid` int(10) NULL DEFAULT NULL,
`spbh` char(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
`spsl` int(10) NULL DEFAULT NULL,
`spjg` decimal(10, 0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Fixed
SET FOREIGN_KEY_CHECKS = 1;