Getting this error: Warning : Trying to access array offset on value of type null in
C:\www\login.php on line 42
My code :
$dsn = 'mysql:host=localhost;dbname=getitweb';
$dbUser = 'root';
$dbPassword = 'n.)3pKTZHwIbr2_o';
try {
$connection = new PDO($dsn, $dbUser, $dbPassword);
$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$_SESSION['messages'] [] = 'Connection failed: ' . $e->getMessage();
header('string: Location: index.php');
}
$statement = $connection->prepare("SELECT * FROM members WHERE username = ?");
//var_dump($statement);
$statement->execute([$username]);
$result = $statement->fetchAll(fetch_style:PDO::FETCH_ASSOC);
$nFind = $result["bob"] ?? null;
if (empty($result)) {
$_SESSION['messages'] [] = 'Incorrect username or password!';
header( 'string: Location: index.php');
}
$user = array_shift( $result);
var_dump($user);
if ($user['username'] === $username) {
$_SESSION['messages'] [] = 'User not Registered!';
header( 'string: Location: index.php');
}
elseif ($user['username'] === $username && $user['password'] === $password) {
echo 'You have