php操作数据库,用wampserver工具

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值