我有一个问题:为什么我不能在脚本中使用方法fetch_object()。我有一个方法:
public function magazyn() {
//return 'magazyn';
$this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `nazwa`");
//$this->magazyn->bind_param('ssss', $id, $nazwa, $kategoria, $sn);
$this->magazyn->execute();
$this->magazyn->store_result();
//return $this->magazyn;
if ($this->magazyn->num_rows > 0) {
$rows = array();
while ($row = $this->magazyn->fetch_object()) {
echo $rows[] = $row;
}
} else {
echo "Brak produktw w bazie";
}
}
脚本返回错误:
致命错误:未捕获错误:调用未定义的方法中的mysqli_stmt::fetch_object()
但我可以使用例如"prepare":
$this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `nazwa`");
不能直接在语句对象上使用fetch_object
。正如这里所说:
是否可以将mysqli_fetch_object与准备好的语句一起使用
和文档中http://php.net/manual/en/mysqli.prepare.php
您应该得到结果,并将fetchobject应用于该结果。
/* execute query */
$stmt->execute();
$result = $stmt->get_result();
/* now you can fetch the results into an object */
while ($myrow = $result->fetch_object()) {