登录到 mySqli 数据库


Logging in into a mySqli database?

Ola,我最近一直在关注本教程 http://www.onlinetuting.com/create-login-script-in-php/

但是我已经做到了,但不得不更改一些东西才能让它连接到我正在使用的服务器,但现在当我使用正确或不正确的电子邮件和密码单击登录时,唯一发生的事情只是弹出说密码不正确,并且永远不会进入博客.php出现此错误:

警告:mysqli_num_rows() 期望参数 1 mysqli_result,布尔值在第 33 行的/SHOME1/sessions/login.php 调用堆栈:0.0013 647688 1 中给出。{主要}()/SHOME1/sessions/login.php:00.0021 652200 2.mysqli_num_rows()/SHOME1/sessions/login.php:33

如果有人理解我遇到的问题?

这是我拥有的 2 个文件:

索引.php

<!DOCTYPE html><?php error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true); session_start();?>
<html>
<head>
<title>User Login</title>
</head>
<body>
<form action="login.php" method="post">
<table width="500" align="center" bgcolor="skyblue">
<tr align="center">
<td colspan="3"><h2>User Login</h2></td>
</tr>
<tr>
<td align="right"><b>Email</b></td>
<td><input type="text" name="email" required="required"/></td> 
</tr>
<tr>
<td align="right"><b>Password:</b></td>
<td><input type="password" name="pass" required="required"></td>
</tr>
<tr align="center">
<td colspan="3">
<input type="submit" name="login" value="Login"/> 
</td>
</tr>
</table>
</form>
</body>
</html>

登录.php

<?php
error_reporting(E_ALL|E_STRICT); ini_set('display_errors', true);
// establishing the MySQLi connection
$hostname = "localhost";
$username = "userexample";
$password = "passexample";
$database = "userdbexample_db1";
$con = new mysqli($hostname, $username, $password, $database);
if (mysqli_connect_errno())
{
    echo "MySQLi Connection was not established: " . mysqli_connect_error();
}
// checking the user
if(isset($_POST['login'])){
$email = mysqli_real_escape_string($con,$_POST['email']);
$pass = mysqli_real_escape_string($con,$_POST['pass']);
$sel_user = "select * from users where user_email='$email' AND user_pass='$pass'";
$run_user = mysqli_query($con, $sel_user);
$check_user = mysqli_num_rows($run_user);
if($check_user>0){
$_SESSION['user_email']=$email;
echo "<script>window.open('blog.php','_self')</script>";
}
else {
echo "<script>alert('Email or password is not correct, try again!')</script>";
}
}
?>

提前感谢!

您的查询失败并且结果对象 $run_user 为空,这就是您收到此错误的原因 我会看看您的查询或确保您传递给查询字符串的变量已设置。