-4

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'];
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Jeroen
  • 161
  • 2
  • 11
  • 5
    You can't mix object-oriented and procedural styles like that. Use `$data->fetch_assoc()` – Niet the Dark Absol Feb 02 '15 at 16:25
  • 1
    possible duplicate of [mysqli\_fetch\_assoc() expects parameter 1 to be mysqli\_result, boolean given](http://stackoverflow.com/questions/11347971/mysqli-fetch-assoc-expects-parameter-1-to-be-mysqli-result-boolean-given) – Rizier123 Feb 02 '15 at 16:25
  • Look http://us3.php.net/manual/en/mysqli.query.php Your SELECT results to false – B001ᛦ Feb 02 '15 at 16:25
  • @bub: uh, no it doesn't result to false. it resulted to an object. – Marc B Feb 02 '15 at 16:27
  • @NiettheDarkAbsol Like "$row = $data->fetch_assoc();" ? Then I get Fatal error: Call to undefined method mysqli_stmt::fetch_assoc() in – Jeroen Feb 02 '15 at 16:35
  • @Rizier123 How is this a duplicate of that? First of all that's not my error and second I'm not using booleans. And that question isn't using a prepared statement. – Jeroen Feb 02 '15 at 16:38
  • *"First of all that's not my error"* - Funny, that's what both the title and what your question contains; so which or whose error is it... *really* then? - *"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`"* – Funk Forty Niner Feb 02 '15 at 16:42
  • @Fred-ii- His error is 'boolean given', mine is 'object given', also I've tried their solutions. – Jeroen Feb 02 '15 at 16:45
  • You may need to post a bit more code then, related to what's been posted in regards to included/required files. If you're using a class which seems to be the case, then add that also. It might also be a scope issue. – Funk Forty Niner Feb 02 '15 at 16:48
  • @Fred-ii- allright I'll see what might be useful, thanks – Jeroen Feb 02 '15 at 16:48
  • If you're not quoting these `(adress, username, password,` then those are being treated as constants if you haven't defined them *from* constants. Otherwise, do as you did for `"trp"` and quote those also. `("adress", "username", "password", "trp")` – Funk Forty Niner Feb 02 '15 at 16:53
  • @Fred-ii- Yes, I did quote those, I just don't want to give my password and info – Jeroen Feb 02 '15 at 16:57
  • Ok. Well, only thing I can think of now is a scope issue which I did mention earlier. Try passing DB connection in your function `public function getItem($mysqli, $id){` – Funk Forty Niner Feb 02 '15 at 16:59
  • Sidenote: You do have a semi-colon at the end of `$post['id']` yes? Just need to dot the i's and bar the t's, as it were ;-) any progress? – Funk Forty Niner Feb 02 '15 at 17:06
  • Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Feb 02 '15 at 17:15

2 Answers2

0

You have to set parameters before executing $result.after

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

you have to set $id value..then you have to run

$result->execute();

if you need more info.please check this link and get a more clear idea.. http://www.w3schools.com/php/php_mysql_prepared_statements.asp

Sanjaya
  • 156
  • 1
  • 9
  • $id is actually set by $id = $_GET['id']; wich I didn't add, I thought that would be obvious. Sorry. – Jeroen Feb 02 '15 at 16:55
0

I got it to work with

$result = $result->get_result(); 
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jeroen
  • 161
  • 2
  • 11