0

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()

danjbh
  • 615
  • 10
  • 21
  • 1
    Sorry, not sure what the question is here – RiggsFolly Jan 03 '17 at 15:37
  • @RiggsFolly I seem to just be having an issue with the one line in my code: `$row = $stmt->fetch_array(MYSQLI_ASSOC);` which seems to be failing me and im not sure what the issue is or the OOP alternative to fetching an array – danjbh Jan 03 '17 at 15:39
  • Try removing the `store_result()` – RiggsFolly Jan 03 '17 at 15:48
  • So that then executes the the `else` statement – danjbh Jan 03 '17 at 15:52
  • A "500 Internal Server Error" status code (or a blank page) means that your script is throwing an error but you haven't configured PHP to display error messages. That's something you need to address before you go further because it's hard to code properly without the aid of error messages. The error reporting thumb rule is to show in development and log in production. As a starting point, I suggest you edit the system-wide `php.ini` file in the computer where you develop and tweak the `error_reporting` and `display_errors` directives ([details here](http://stackoverflow.com/a/5680885/13508)). – Álvaro González Jan 03 '17 at 15:52
  • @ÁlvaroGonzález I do have error reporting set - Ill add the error to my question. – danjbh Jan 03 '17 at 15:53
  • You said "just serves a 500" but didn't share any error message. I assume you were getting none. – Álvaro González Jan 03 '17 at 15:54
  • @ÁlvaroGonzález why are you marking this as duplicate im trying to understand what the problem is and this seems to be like your hindering? – danjbh Jan 03 '17 at 15:56
  • Because that's the same question and even has an answer? – Álvaro González Jan 03 '17 at 15:58
  • I mention that very same thing in my question but without understanding what the problem is the other questions is very loosely related. – danjbh Jan 03 '17 at 15:58
  • If i swap `$row = $stmt->fetch_array(MYSQLI_ASSOC);` to `$row = $stmt->mysqli_stmt::fetch();` no errors are reported it serves a 500 suggesting a synstax error hence why I need to understand why I am not asking for an answer I am simply asking for advice. – danjbh Jan 03 '17 at 16:02

0 Answers0