在 php 中使用 num_rows 方法时出错

An error while using num_rows method in php

我设计了一个登录表单,但在处理它时出现了一个致命错误,我不知道如何解决。 这是我的代码:

dbConnection.php:

<?php 
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'fashionshop';

$conn = new mysqli($host,$user,$pass,$dbname);

if($conn->connect_error){
    die('Connection Failed:' .$conn->connect_error);
}
//echo "Connected Successfully";
?>

userloginprocess.php:

<?php require_once 'dbConnection.php'; ?>

<?php

if(isset($_POST['client-login-submit'])){
    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM users
        WHERE username = '".$username."' AND password1 = '".$password."' LIMIT 1";

    $result = $conn->query($sql);

    if($conn->num_rows($result) == 1){  // this is line 14
        header('location:welcome.php');
    }else{
        header('location:error.php');
    }
}else{
    header('location:notworking.php');
}

?>

html形式:

<form action="inc/userloginprocess.php" method="post">
    <h1>LOG IN</h1>
    <input placeholder="Username" type="text" name="username" id="username">
    <input placeholder="Password" type="password" name="password" id="password">
    <input type="submit" name="client-login-submit" value="LOG IN">
    <p>Forget Your Password? <a href="#">RESET</a></p>
    <p><a href="#">SIGN UP</a></p>
</form>

最后提交表单时出现错误:

Fatal error: Uncaught Error: Call to undefined method mysqli::num_rows() in C:\xampp\htdocs\fashionshop\inc\userloginprocess.php:14 Stack trace: #0 {main} thrown in C:\xampp\htdocs\fashionshop\inc\userloginprocess.php on line 14

如果你能帮助我,我将不胜感激。

您在获取行数时使用了错误的对象。使用 $result 而不是 $conn.

制作

$conn->num_rows($result)

收件人:

$result->num_rows