dsjq62428
dsjq62428
2015-02-02 16:21

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,给定对象

已采纳

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 wich 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'];
}

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

config.php wich 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'];
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duan_88598 duan_88598 6年前

    Welp, I got it to work with

    $result = $result->get_result(); 
    

    Thanks for all the help!

    点赞 评论 复制链接分享
  • duanou9739 duanou9739 6年前

    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

    点赞 评论 复制链接分享

为你推荐