【发布时间】: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'];
}
【问题讨论】:
-
你不能像这样混合面向对象和过程风格。使用
$data->fetch_assoc() -
看us3.php.net/manual/en/mysqli.query.php你的SELECT结果为假
-
@bub:嗯,不,它不会导致错误。结果是一个对象。
-
@MarcB 哦,是的,谢谢
标签: php sql arrays mysqli prepared-statement