SELECT COUNT (a.id) AS id ... => the result array is empty! [PHP, MySQL]

I had problems with some PHP from yesterday, browsed the Internet and it was stupid that I was missing something important.

Using mysql_fetch_objectusually, I tried it with mysql_fetch_arraythough (did not help). Here is the piece of code that gives me a 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 with the help it print_r $arrayalready shows you that the array remains empty (0). I am sure that an error appears in this block of code, can the error " "? COUNT (x) AS y

PS: MySQL Query works, tested it through Workbench before. I would appreciate a good attitude! :-)

Have a nice day!

+3
source share
2 answers

Isn't it that simple:

$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.' to ' ". $id." '. ". $Id.".

0

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

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

1

0