【问题标题】:Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,给定对象
【发布时间】:2015-04-01 15:45:43
【问题描述】:

我收到此错误:

警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,对象在第 14 行给出 \Views\item.view.php

这是一个准备好的语句,它返回 1 行。但是,我无法从中获取任何数据。我在这里尝试了很多其他问题的解决方案,但没有奏效。我觉得我错过了一些小东西。

这是 item.php 的一部分

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

这是 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;
        
    }

还有风景

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

编辑:我也试过

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

   $post['title'];
}

这给了 致命错误:调用未定义的方法 mysqli_stmt::fetch_assoc()

config.php 包含在内,参数隐藏

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

EDIT2:item.class.php 中的这个方法确实有效

   public function getItems(){

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

}

在视图中

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

编辑: 明白了

$result = $result->get_result();

在视图中

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

        echo $post['subject'];
}

【问题讨论】:

标签: php sql arrays mysqli prepared-statement


【解决方案1】:

我可以使用它

$result = $result->get_result(); 

【讨论】:

    【解决方案2】:

    在执行$result.after之前必须设置参数

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

    你必须设置 $id 值..然后你必须运行

    $result->execute();
    

    如果您需要更多信息。请检查此链接并获得更清晰的想法.. http://www.w3schools.com/php/php_mysql_prepared_statements.asp

    【讨论】:

    • $id 实际上是由 $id = $_GET['id'];我没有添加,我认为这很明显。对不起。
    猜你喜欢
    • 2022-12-31
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 2015-03-12
    相关资源
    最近更新 更多