問題描述
我正在為 uni 開發一個項目,并且一直在 testing server
上使用以下代碼從基于 的表中
:獲取所有設備
user_id
I'm working on a project for uni and have been using the following code on a testing server
to get all devices
from a table based on a user_id
:
public function getAllDevices($user_id) {
$stmt = $this->conn->prepare("SELECT * FROM devices WHERE primary_owner_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$devices = $stmt->get_result();
$stmt->close();
return $devices;
}
這在我的測試服務器上運行良好,但在遷移到大學項目服務器時返回此錯誤:
This worked fine on my testing server but returns this error when migrating over to the university project server:
Call to undefined method mysqli_stmt::get_result()
一些谷歌搜索建議使用 bind_result()
而不是 get_result()
但我不知道如何在表中執行此all fields
.大多數示例只顯示返回 one field
Some googling suggests using bind_result()
instead of get_result()
but I have no idea how to do this all fields
in the table. Most examples only show returning one field
任何幫助將不勝感激
推薦答案
假設你不能使用 get_result()
并且你想要一個設備數組,你可以這樣做:
Assuming you can't use get_result()
and you want an array of devices, you could do:
public function getAllDevices($user_id) {
$stmt = $this->conn->prepare("SELECT device_id, device_name, device_info FROM devices WHERE primary_owner_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$stmt->bind_result($id, $name, $info);
$devices = array();
while($stmt->fetch()) {
$tmp = array();
$tmp["id"] = $id;
$tmp["name"] = $name;
$tmp["info"] = $info;
array_push($devices, $tmp);
}
$stmt->close();
return $devices;
}
這會創建一個臨時數組并存儲其中每一行的數據,然后將其推送到主數組.據我所知,您不能在 bind_result()
中使用 SELECT *
.相反,您將不得不在 SELECT
This creates a temporary array and stores the data from each row in it, and then pushes it to the main array. As far as I'm aware, you can't use SELECT *
in bind_result()
. Instead, you will annoyingly have to type out all the fields you want after SELECT
這篇關于如何在 php 中使用 bind_result() 而不是 get_result()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!