Search code examples
phpsqlarraysmysqliprepared-statement

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given


I am getting this error:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given \Views\item.view.php on line 14

It's a prepared statement which returns 1 row. However, I can't get any data from it. I've tried a lot of solutions from other questions here, but they didn't work. I feel like I'm missing something small.

This is a part of item.php

$id = $_GET['id'];
$data = $item->getItem($id);
require 'Views/item.view.php';

This is item.class.php

public function getItem($id){
        include('/../config.php'); //contains the $mysqli
        
        $result = $mysqli->prepare("SELECT * FROM item WHERE id = ?") ;

        $result->bind_param("i", $id);

        $result->execute();
        
        
        if (!$result ) {
            return false;
        }
        return $result;
        
    }

And the view

$row = mysqli_fetch_assoc($data);
                        $row['title'];

EDIT: I also tried

while($post = $data->fetch_assoc()){ 

   $post['title'];
}

Which gives Fatal error: Call to undefined method mysqli_stmt::fetch_assoc()

config.php which is included, parameters hidden

$mysqli = new mysqli(adress, username, password, "trp");

EDIT2: This method from item.class.php does work

   public function getItems(){

    include('/../config.php');
    
    $sql ="SELECT * FROM item";
    $result = $mysqli->query($sql);
    
    if (!$result ) {
        return false;
    }
    return $result;

}

With in the view

while($post = $data->fetch_assoc()){ 
$post['id']
}

EDIT: Got it with

$result = $result->get_result();

and in the view

    while($post = $data->fetch_assoc()){ 

        echo $post['subject'];
}

Solution

  • I got it to work with

    $result = $result->get_result();