如何在php中使用bind_result()而不是get_result()

Ron*_*ins 4 php mysql sql mysqli prepared-statement

我正在为单向的项目,并一直使用在下面的代码testing server,以get all devices基于一个表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;
}
Run Code Online (Sandbox Code Playgroud)

这在我的测试服务器上工作正常,但在迁移到大学项目服务器时返回此错误:

Call to undefined method mysqli_stmt::get_result()
Run Code Online (Sandbox Code Playgroud)

一些谷歌搜索建议使用bind_result()而不是get_result()但我不知道如何all fields在表中这样做.大多数示例仅显示返回one field

任何帮助将非常感激

Jos*_*rts 7

假设你不能使用get_result()并且想要一组设备,你可以这样做:

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;
}
Run Code Online (Sandbox Code Playgroud)

这将创建一个临时数组并存储其中每行的数据,然后将其推送到主数组.据我所知,你不能使用SELECT *bind_result().相反,你会烦恼地输入你想要的所有字段SELECT