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

  • Post author:
  • Post category:php


假设你不能使用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;

}

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



版权声明:本文为weixin_34487308原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。