默认MySQL环境没有配置密码
首先每个调用MySQL数据库的php code
需要在最前面登陆和定义你要链接的库like this
1 2 3 4 5 6 7 8 9 10 11
| <?php $servername = "localhost"; $serverRootName = "root"; $serverPasswd = ""; $db = "mydb"; $mysql = new mysqli($servername, $serverRootName, $serverPasswd, $db); if($mysql->connect_error){ die("链接失败: ". mysqli_connect_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 = ?";
|
然后是预处理!
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){ 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();
|
完整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"; $serverPasswd = ""; $db = "mydb"; $mysql = new mysqli($servername, $serverRootName, $serverPasswd, $db); if($mysql->connect_error){ die("链接失败: ". mysqli_connect_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();
|