php操作数据库,用wampserver工具
打开wampserver数据库可视化,创建表格,插入数据
DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user` (
`user_Id` int NOT NULL AUTO_INCREMENT COMMENT '用户编号',
`user_Name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '用户姓名',
`user_Pwd` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '用户密码',
`gender` int DEFAULT NULL COMMENT '性别',
`brief` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin COMMENT '简介',
PRIMARY KEY (`user_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
INSERT INTO `user` (`user_Id`, `user_Name`, `user_Pwd`, `gender`, `brief`) VALUES
(1, '张三', '123456', 1, '我是一个懂法律的人'),
(2, '麻子', '123456', 1, '一个很保守的人'),
(3, '李四', '123456', 0, '一个很人');
COMMIT;
创建php文件操作数据库
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
$conn = new mysqli($servername, $username, $password,$dbname);
if ($conn->connect_error) {
die("连接失败" . $conn->connect_error);
}
$sql = "INSERT into user(user_name,user_pwd,gender,brief)values('李四',123456,0,'一个很的人')";
if ($conn->query($sql) === TRUE) {
echo "新纪录插入成功";
} else {
echo "Error:" . $sql . "<br>" . $conn->error;
}
$conn->close(); //关闭连接
?>```