0

I am trying to login users but I have this error message : Fatal error: Uncaught Error: Call to a member function fetchAll() on bool

Maybe it's because the $result variable is returning 1 (bool). But I don't know how to fix this. Any help is welcome.

            $sql = "SELECT * FROM users WHERE username = ?";
            $stmt = $db->prepare($sql);
            $stmt->bind_param("s", $_POST['name']);
            $result = $stmt->execute();
            print_r($result); // returns 1
            $users = $result->fetchAll();

            if (isset($users[0])) {
                if (password_verify($_POST['password'], $users[0]->password)) {
                    $_SESSION['username'] = $username;
                    header('location: index.php'); 
                } 
                else {
                    echo "Wrong password";
                }
            } 
            else {
                echo "Wrong username";
            }
Dharman
  • 30,962
  • 25
  • 85
  • 135
YapYap31
  • 1
  • 2

1 Answers1

-1

I think that you want to build an array with all usernames in the first section of your code (up to fetchAll()). To acchieve this try the following:

<?php
$users = array();
$con = sql_connect($servername, $username, $password, $db);
$stmt = $con->prepare("SELECT `username` FROM `users`");
$stmt->execute();
foreach ($stmt->get_result() as $row)
{
    $users[] = $row['users'];
}
mysqli_close($con);

if(in_array($_POST['name'], $users)
{
....
}
else
{
....
}
?>
rauwitt
  • 312
  • 2
  • 4