PHP mysqli_fetch_assoc returns null

26 Views Asked by At

So im trying to make a basic login page but I get this error:

Warning: Trying to access array offset on value of type null

Note: Im a beginner to PHP so please try not to overcomplicate things a little bit

This is my PHP Code:

if ($_SERVER["REQUEST_METHOD"] == "POST"){
        $username = trim(filter_input(INPUT_POST, "username", FILTER_SANITIZE_SPECIAL_CHARS));
        $password = filter_input(INPUT_POST, "password", FILTER_SANITIZE_SPECIAL_CHARS);

        $sql = "SELECT * FROM users WHERE name = '{$username}'";
        $result = mysqli_query($conn, $sql);
        if(mysqli_fetch_assoc($result) > 0){
            $row = mysqli_fetch_assoc($result);
            if (password_verify($password, $row["password"])){
                session_start();
                session_regenerate_id();
                $_SESSION["user_id"] = $row["id"];

                header("Location: home.php");
                exit;
            } else {
                echo'<div style="background-color:crimson;">Incorrect Password</div>';
            }
        } else {
            echo'<div style="background-color:crimson;">User not Found</div>';
        }
    }

0

There are 0 best solutions below