如何在PHP中使用bind_result()代替get_result() [英] How to use bind_result() instead of get_result() in php

查看:264
本文介绍了如何在PHP中使用bind_result()代替get_result()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为uni开发项目,并且一直在基于user_id的表中的testing serverget all devices上使用以下代码:

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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆