我正在使用两个查询从我的数据库中获取,当我尝试在每个查询中打印结果时,它确实打印了所有结果,但是当我尝试在外部打印时,它并没有打印所有内容:$arr_devices = [];$sql1 = "SELECT id FROM users WHERE users.name = '$company_name'"; //finding the ids of the users of this companyif ($res = $db->query($sql1)) { $arr_id = $res->fetch_all(MYSQLI_ASSOC); foreach ($arr_id as $company_id) { $sql = "SELECT devices.id, devices.serial_imei, devices.serial_no, devices.type_id, devices.cus_id, devices.sales_date, device_types.name, device_types.manufacturer, device_types.device_no, device_types.barcode, users.id, users.name as manufacturer_name FROM devices INNER JOIN device_types ON device_types.id = devices.type_id INNER JOIN users ON device_types.manufacturer = users.id WHERE devices.cus_id = " . $company_id['id'] . " "; $result = $db->query($sql); if ($result->num_rows > 0) { $arr_devices = $result->fetch_all(MYSQLI_ASSOC); } print_r($arr_devices); //HERE IT PRINTS EVERYTHING I AM FETCHING } print_r($arr_devices); //HERE IT DOESNT}
1 回答
拉丁的传说
TA贡献1789条经验 获得超8个赞
您在每个循环中都覆盖数组。
采用$arr_devices[] = $result->fetch_all(MYSQLI_ASSOC);
将查询结果附加到数组。
作为旁注,在循环内使用查询通常是不好的,因为它会要求你的数据库太多。仅使用一个查询:
SELECT devices.id,
devices.serial_imei,
devices.serial_no,
devices.type_id,
devices.cus_id,
devices.sales_date,
device_types.name,
device_types.manufacturer,
device_types.device_no,
device_types.barcode,
users.id,
users.name as manufacturer_name
FROM devices
INNER JOIN device_types
ON device_types.id = devices.type_id
INNER JOIN users ON device_types.manufacturer = users.id
WHERE devices.cus_id IN (SELECT id FROM users WHERE users.name = '$company_name')
然后您可以在一个查询中获取所有内容,速度要快得多。
- 1 回答
- 0 关注
- 66 浏览
添加回答
举报
0/150
提交
取消