简体   繁体   English

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

[英]SELECT COUNT(a.id) AS id… => Result array is empty! [PHP, MySQL]

I'm having trouble with some PHP since yesterday, looked through the web and had the stupid feeling that I'm missing something important. 我从昨天起就遇到了一些PHP问题,浏览网页并感到愚蠢的感觉我错过了一些重要的东西。

Using mysql_fetch_object usually, tried it with mysql_fetch_array though (did not help). 通常使用mysql_fetch_object ,尝试使用mysql_fetch_array (但没有帮助)。 Here's the part of the code which gives me an headache: 这是代码的一部分让我头疼:

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 is used in another .php file, but using print_r $array already shows you, that the array stays empty (0) . $ array用于另一个.php文件,但是使用print_r $array已经向您显示该数组保持为empty (0) I'm quite sure that the error appears in this block of code, could " COUNT (x) AS y " be at fault? 我很确定错误出现在这段代码中,可能“ COUNT (x) AS y ”有问题吗?

PS: The MySQL Query works, tested it via Workbench before. PS:MySQL Query工作,之前通过Workbench测试过。 I'd appreciate some good adivce! 我很欣赏一些好的adivce! :-) :-)

Have a nice day! 祝你今天愉快!

Instead of using it like this 而不是像这样使用它

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

You should do this 你应该做这个

$row = mysql_fetch_object($result)
$array[] = $row->Racks;
$array[] = $row->Devices;
$array[] = $row->Cards;

Because you are fetching 1 record and using it in while is causing problem 因为您正在获取1条记录并在使用它时导致问题

Isn't this as simple as: 这不是简单的:

$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());

Note the modification from '.$id.' 请注意'。$ id。'的修改。 to '" . $id . "'. to'“。$ id。”'。 Your query is looking for a data centre ID of '.$id.'. 您的查询正在查找数据中心ID“。$ id。”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM