gpt4 book ai didi

php - SELECT COUNT(a.id) AS id... => 结果数组为空! [PHP, MySQL]

转载 作者:搜寻专家 更新时间:2023-10-31 22:13:05 25 4
gpt4 key购买 nike

从昨天开始,我在使用某些 PHP 时遇到了问题,浏览了 web 并有一种愚蠢的感觉,我错过了一些重要的东西。

通常使用 mysql_fetch_object,但尝试使用 mysql_fetch_array(没有帮助)。这是让我头疼的代码部分:

public static function get_datacenter_by_id($id) {
$result = mysql_query("SELECT COUNT(rack.id) AS Racks, COUNT(device.id) AS Devices, COUNT(card.id) AS Cards, COUNT(port.id) AS Ports
FROM datacenter, rack, device, card, port, location, building
WHERE location.id = building.location_id AND
building.id = datacenter.building_id AND
datacenter.id = '.$id.' AND
rack.id = device.rack_id AND
device.id = card.device_id AND
(card.id = port.card_id1 OR
card.id = port.card_id2)") or die ("Error in query: ".mysql_error());

$array = array();

while($row = mysql_fetch_object($result)) {
$array[] = array($row->Racks, $row->Devices, $row->Cards, $row->Ports);
}

return $array;
}

$array 在另一个 .php 文件中使用,但是使用 print_r $array 已经向您展示了该数组保持为 empty (0)。我很确定错误出现在这段代码中,“COUNT (x) AS y”是否有问题?

PS:MySQL 查询有效,之前通过 Workbench 对其进行了测试。我会很感激一些好的建议! :-)

祝你有美好的一天!

最佳答案

这不是很简单吗:

$result = mysql_query("SELECT COUNT(rack.id) AS Racks, COUNT(device.id) AS Devices,     COUNT(card.id) AS Cards, COUNT(port.id) AS Ports 
FROM datacenter, rack, device, card, port, location, building
WHERE location.id = building.location_id AND
building.id = datacenter.building_id AND
datacenter.id = '" . $id . "' AND
rack.id = device.rack_id AND
device.id = card.device_id AND
(card.id = port.card_id1 OR
card.id = port.card_id2)") or die ("Error in query: ".mysql_error());

注意来自“.$id”的修改。到 '".$id."'。您的查询正在查找“.$id.”的数据中心 ID。

关于php - SELECT COUNT(a.id) AS id... => 结果数组为空! [PHP, MySQL],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10329276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com