dsjq62428 2015-02-02 16:21
浏览 113
已采纳

警告: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 2015-02-02 19:19
    关注

    Welp, I got it to work with

    $result = $result->get_result(); 
    

    Thanks for all the help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程