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(); //关闭连接
?>
<?php
header('content-type:text/html;charset=utf-8');
$host = "localhost";
$user = "root";
$password = "";
$db = "mydb";
$mysqli = mysqli_connect($host, $user, $password, $db);
if (mysqli_connect_errno()) {
echo "连接成功" . mysqli_connect_error();
}
$mysqli->set_charset("utf8");
$sql = "SELECT user_Name,user_Pwd FROM user WHERE user_Id=?";
$mysqli_stmt = $mysqli->prepare($sql);
$id = 1;
$mysqli_stmt->bind_param("i", $id);
if ($mysqli_stmt->execute()) {
$mysqli_stmt->bind_result($user_Name,$user_Pwd);
while ($row = $mysqli_stmt->fetch()) {
echo "姓名:" . $user_Name;
echo "密码:". $user_Pwd;
}
}
$mysqli_stmt->free_result();
$mysqli_stmt->close();
$mysqli->close();