我正在尝试从MySQL数据库中收集一些信息并遇到一些问题。我可以独立执行我的两个查询中的任何一个,但是当我尝试执行它们时,我的代码都会失败并显示错误Call to a member function bind_param() on a non-object in /api/boxes.php on line 20
,其中第20行是$queryFacet = $db->prepare("SELECT item_name FROM kf_facet_items WHERE id IN (SELECT facet_value_id FROM kf_box_data WHERE box_id = ?)");
这是我的代码:
$query = $db->prepare("SELECT DISTINCT b.id, b.ownerID, b.transactionID, b.post_date, b.description FROM kf_boxes b JOIN kf_box_data d ON b.id = d.box_id");
$query->execute();
$query->bind_result($id, $owner, $transactionID, $post_date, $description);
while ($query->fetch()) {
$result['id'] = $id;
$result['transaction'] = $transactionID;
$result['post_date'] = $post_date;
$result['description'] = $description;
$queryFacet = $db->prepare("SELECT item_name FROM kf_facet_items WHERE id IN (SELECT facet_value_id FROM kf_box_data WHERE box_id = ?)");
$queryFacet->bind_param('s',$id);
$queryFacet->execute();
$queryFacet->bind_result($item_name);
while ($queryFacet->fetch()) {
$facets['name'] = $item_name;
$result['facets'] = $facets;
}
$output[] = $result;
}
任何人对导致这种情况的原因有什么想法,更重要的是,如何解决?
答案 0 :(得分:0)
问题解决了。我没有关闭我的第一个声明,它让mysqli感到头晕目眩。在while语句之后添加$query->close();
,然后为第二个查询添加for循环按预期工作。