在 php oop 中使用方法 mysqli
Use method mysqli in php oop
我有一个问题:为什么我不能在我的脚本中使用方法 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";
}
}
脚本return错误:
Fatal error: Uncaught Error: Call to undefined method
mysqli_stmt::fetch_object() in
但我可以使用 "prepare":
$this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `nazwa`");
不能直接在语句对象上使用 fetch_object
。正如这里所说:
Is it possible to use mysqli_fetch_object with a prepared statement
并在文档中
http://php.net/manual/en/mysqli.prepare.php
您应该得到结果并对其应用 fetch_object。
/* execute query */
$stmt->execute();
$result = $stmt->get_result();
/* now you can fetch the results into an object */
while ($myrow = $result->fetch_object()) {
我有一个问题:为什么我不能在我的脚本中使用方法 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";
}
}
脚本return错误:
Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_object() in
但我可以使用 "prepare":
$this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `nazwa`");
不能直接在语句对象上使用 fetch_object
。正如这里所说:
Is it possible to use mysqli_fetch_object with a prepared statement
并在文档中 http://php.net/manual/en/mysqli.prepare.php
您应该得到结果并对其应用 fetch_object。
/* execute query */
$stmt->execute();
$result = $stmt->get_result();
/* now you can fetch the results into an object */
while ($myrow = $result->fetch_object()) {