我已经到处搜索,但找不到问题的解决方案。我在 php 中有一个登录脚本。当数据库中没有用户输入的值匹配时,会显示一条错误消息。但还会引发另一个警告。
Warning: mysqli_num_rows() expects parameter 1 to be mysqli,
boolean given in C:\xampp\htdocs\index.php on line 26
这是代码。我知道类似的问题已被问过很多次,但没有一个可以为我提供解决方案。这是整个代码。
<?php
session_start();
require_once('connectionvariables.php');
$error_msg = "";
if(!isset($_SESSION['user_id']))
{
if(!empty($_COOKIE['user_id']) && !empty($_COOKIE['username']))
{
$_SESSION['user_id'] = $_COOKIE['user_id'];
$_SESSION['username'] = $_COOKIE['username'];
}
else
{
if(isset($_POST['submit']))
{
$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$user_username = mysqli_real_escape_string($dbc,trim($_POST['username']));
$user_password = mysqli_real_escape_string($dbc,trim($_POST['password']));
if(!empty($user_username) && !empty($user_password))
{
$query = "SELECT user_id,username FROM mismatch_user WHERE username = '$user_username' AND password = SHA('$user_password')";
$data = mysqli_query($dbc,$query);
if(mysqli_num_rows($data) == 1)
{
$row = mysqli_fetch_array($data);
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['username'] = $row['username'];
setcookie('user_id',$row['user_id'],time() + ( 60 * 60 * 3600));
setcookie('username',$row['username'],time() + ( 60 * 60 * 3600));
header('Location: http://www.google.com');
}
else
{
$error_msg = 'enter a valid username and password';
}
}
else
{
$error_msg = 'you have to enter username and password to Log In...';
}
}
}
}
?>
这是第 26 行。
if(mysqli_num_rows($data) == 1)