I am new to OOP PHP and am struggling to understand the alternative to using:
$stmt->fetch_array(MYSQLI_ASSOC);
This line seems to be the offender in the example below:
<?php
include '../config.php';
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
// Collect the form inputs
$email = $_POST['email'];
$password = $_POST['password'];
// Prepare and store the inputs client side
if($stmt = $conn->prepare("SELECT * FROM users WHERE email = ?"))
{
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
}
// Check if the user exists
if($stmt->num_rows === 1)
{
$row = $stmt->fetch_array(MYSQLI_ASSOC);
if (password_verify($password, $row['password']))
{
// Passing sesion data through for us to use
$_SESSION['id'] = $row[ 'user_id'];
$_SESSION['email'] = $row[ 'user_email'];
header("Location: index.php");
}
}
else
{
echo 'Something has gone wrong';
}
// Close and clense any open connections
$stmt->close();
$conn->close();
?>
I am playing around trying to understand prepared statements and this line is causing me some grief!
I tried to look for the soliton myself and lots of articles suggest using mysqli_stmt::fetch()
but this just serves a 500
My question is what is the correct way to assign an array the $row
variable because when using the above I get an error served.
I see the following error with errors enabled:
Fatal error: Call to undefined method mysqli_stmt::fetch_array()