'Error on Login-Page "Warning: Trying to access array offset on value of type bool" [duplicate]

I am currently making a website for a school project and I am making a user registration system. Currently, the sign-up portion works perfectly with user data going into the MySQL database. However, my login-page seems to be broken. Every time I try to log-in I get the following error:

Warning: Trying to access array offset on value of type bool in D:\XAMPP\htdocs\hennorist\login.php on line 23

Attached is the PHP code in question:

<?php
    
require_once "config.php";
require_once "session.php";

$error = '';
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
    
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);
    
    //validate if email is empty
    if (empty($email)) {
        $error .= '<p class="error">Please enter email.</p>';
    }
    
    if (empty($error)) {
        if($query = $db->prepare("SELECT * FROM users WHERE email = ?")) {
            $query->bind_param('s', $email);
            $query->execute();
            $row = $query->fetch();
            if ($row) {
                if (password_verify($password, $row['password'])) {
                    $_SESSION["userid"] = $row['id'];
                    $_SESSION["user"] = $row;
                    
                    //Redirect user to welcome page
                    header("location: exclusive.php");
                    exit;
                } else {
                    $error .= '<p class="error">The password is not valid.</p>';
                }
            } else {
                $error .= '<p class="error">No user exists with that email address.</p>';
            }
        }
        $query->close();
    }
    //Close connection
    mysqli_close($db);
}
?>


Solution 1:[1]

Are you forced to work with the PDO driver? as a beginner it will be a little tricky for you but I would suggest you to work with MySQLi instead,

In your included file for connexion (guess config.php) to database make sure your connexion is instancing the PDO class as $query = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

Then to close connexion use $query = null; instead of mysqli_close($db);

Assume you understood try executing the following code

 <?php
    
require_once "config.php";
require_once "session.php";

$error = '';
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
    
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);
    
    //validate if email is empty
    if (empty($email)) {
        $error .= '<p class="error">Please enter email.</p>';
    }
    
    if (empty($error)) {
        if($query = $db->prepare("SELECT * FROM users WHERE email = ?")) {
            $query->bind_param('s', $email);
            $query->execute();
            $row = $query->fetch(PDO::FETCH_ASSOC);
            if ($row) {
                if (password_verify($password, $row['password'])) {
                    $_SESSION["userid"] = $row['id'];
                    $_SESSION["user"] = $row;
                    
                    //Redirect user to welcome page
                    header("location: exclusive.php");
                    exit;
                } else {
                    $error .= '<p class="error">The password is not valid.</p>';
                }
            } else {
                $error .= '<p class="error">No user exists with that email address.</p>';
            }
        }
    }
    //Close connection
    $query=null;
}
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kernel