PHP注册页面连接mysql数据库的方法
通过在注册页面源码中添加以下代码即可连接到MySQL数据库
<!DOCTYPE html><html><head>
<meta charset="UTF-8">
<title>登录</title></head><body>
<p>
<b>登录</b>
<form action="login.php" method="post">
<p><span>用户名:</span><input type="text" name="user" required="" /></p>
<p><span>密码:</span><input type="password" name="pass"/ required=""></p>
<p><input type="reset" name=""/> <input type="submit" name=""/></p>
</form>
<a href="register.html">新用户注册</a>
</p></body></html>
login.php
<meta charset="utf-8"><?php
$conn = mysqli_connect('localhost','root','password','db_name') or die('数据库连接失败');
$conn->query("SET NAMES 'UTF8'");
$user = $_POST['user'];
$pass = $_POST['pass'];
$sql="SELECT * FROM users where user='{$user}' and pass='{$pass}'";
$result=$conn->query($sql);
$row = mysqli_num_rows($result);
//若表中存在输入的用户名和密码,row=1;若表中用户名不存在或密码错误,则row=0
if($row == 1){
echo $row['user']."登陆成功!";
}
else{
echo"登录失败,请重新登录!";
}?>