默认MySQL环境没有配置密码

首先每个调用MySQL数据库的php code
需要在最前面登陆和定义你要链接的库like this

1
2
3
4
5
6
7
8
9
10
11
<?php
$servername = "localhost";//域名
$serverRootName = "root";//mysqli账号 (root)
$serverPasswd = "";//mysqli账号的密码 (root)
$db = "mydb";//数据库名
$mysql = new mysqli($servername, $serverRootName, $serverPasswd, $db); //链接mysqli
if($mysql->connect_error){
die("链接失败: ". mysqli_connect_error()); //如果失败输出error
}
$mysql->set_charset("utf8");
?>

这样我们就可以连上MYSQL啦
我们现在就可以把我们之前做的登陆连上数据库啦
我们需要先建立一个function

1
2
3
4
function getUser($mysql)
{

}

然后再往里面加内容!
我们需要一个MySQL查询语句来查询数据库

1
2
$sql = "SELECT uid , username , gender , brief , password , ID , admin FROM user WHERE username = ? and password = ?";
// 这里我的数据库里有uid username gender brief password ID 和一个管理员登陆判定 然后查询的位置就是这两个问号 后面会定义这两个问号

然后是预处理!

1
$mysqli_stmt = $mysql->prepare($sql); //预处理

获取网页输入 执行查询指令

1
2
3
4
$username = $_POST["username"]; //网页输入的用户名
$passwd = $_POST["pword"]; //网页输入的密码

$mysqli_stmt->bind_param("ss",$username,$passwd); //执行查询命令

然后是判断是否执行 然后检测获取数据数据库是否返回空

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
if($mysqli_stmt->execute()) //是否执行
{
//预定义
$dbuid = null;
$dbusername = null;
$dbgender = null;
$dbbrief = null;
$dbpassword = null;
$dbID = null;
$dbadmin = null;

$mysqli_stmt->bind_result($dbuid,$dbusername,$dbgender,$dbbrief,$dbpassword,$dbID,$dbadmin); //获取查询结果
while ($mysqli_stmt->fetch() && $dbusername !== null && $dbpassword !== null){ //判定如果返回不为null
if($dbadmin == 1){ //如果是管理员登陆 跳转至管理页
echo "<script>alert('欢迎回来 Admin!');window.location.href='admin.html'</script>";
}else{ // 否则为用户登陆 输出用户基本信息
echo "欢迎回来";
echo "<br/>ID:".$dbID;
echo "<br/>用户名:".$dbusername;
if ($dbgender ==1){$gender="男";}elseif($dbgender ==0){$gender="女";}elseif($dbgender ==2){$gender="未知";}elseif($dbgender ==3){$gender="武装直升机";}else{$gender="Null";}
echo "<br/>性别:".$gender;
echo "<br/>简介:".$dbbrief;
echo "<br/>密码:".$dbpassword;
echo "<script>alert('欢迎回来 User')</script>";
}
}if($dbusername == null && $dbpassword == null){ //如果返回为null 则重跳转
echo "<script>alert('密码或者用户名错误!');window.location.href='login.html'</script>";
}

}
}
$mysql->close();//关闭链接

完整code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
$servername = "localhost";//服务器名
$serverRootName = "root";//mysqli账号 (root)
$serverPasswd = "";//mysqli账号的密码 (root)
$db = "mydb";//数据库名
$mysql = new mysqli($servername, $serverRootName, $serverPasswd, $db); //链接mysqli
if($mysql->connect_error){
die("链接失败: ". mysqli_connect_error()); //如果失败输出error
}
$mysql->set_charset("utf8");
getUser($mysql);

function getUser($mysql)
{
$sql = "SELECT uid , username , gender , brief , password , ID , admin FROM user WHERE username = ? and password = ?";
$mysqli_stmt = $mysql->prepare($sql);

$username = $_POST["username"];
$passwd = $_POST["pword"];

$mysqli_stmt->bind_param("ss",$username,$passwd);


if($mysqli_stmt->execute())
{
$dbuid = null;
$dbusername = null;
$dbgender = null;
$dbbrief = null;
$dbpassword = null;
$dbID = null;
$dbadmin = null;

$mysqli_stmt->bind_result($dbuid,$dbusername,$dbgender,$dbbrief,$dbpassword,$dbID,$dbadmin);
while ($mysqli_stmt->fetch() && $dbusername !== null && $dbpassword !== null){
if($dbadmin == 1){
echo "<script>alert('欢迎回来 Admin!');window.location.href='admin.html'</script>";
}else{
echo "欢迎回来";
echo "<br/>ID:".$dbID;
echo "<br/>用户名:".$dbusername;
if ($dbgender ==1){$gender="男";}elseif($dbgender ==0){$gender="女";}elseif($dbgender ==2){$gender="未知";}elseif($dbgender ==3){$gender="武装直升机";}else{$gender="Null";}
echo "<br/>性别:".$gender;
echo "<br/>简介:".$dbbrief;
echo "<br/>密码:".$dbpassword;
echo "<script>alert('欢迎回来 User')</script>";
}
}if($dbusername == null && $dbpassword == null){
echo "<script>alert('密码或者用户名错误!');window.location.href='login.html'</script>";
}

}
}
$mysql->close();//关闭链接