num_rows === 0) { echo '{"error":"device-id not found", "device-id":"' . $device_id . '"}'; exit; } $size = $query->num_rows; $str = '{"alarms": ['; $count = 1; while ($row = $query->fetch_assoc()) { $time = $row['time']; $timea = explode(":", $time); $hour = $timea[0]; $minute = $timea[1]; $str .= '{"id":"' . $row["id"] . '", '; $str .= '"name":"' . $row["name"] . '", '; $str .= '"hour":"' . $hour . '", '; $str .= '"minute":"' . $minute. '", '; $str .= '"ip":"' . $row["ip"] . '", '; $str .= '"port":"' . $row["port"] . '", '; $str .= '"path":"/' . $row["path"] . '"'; if($count == $query->num_rows) { $str .= '}'; } else { $str .= '}, '; } $count++; } $str .= ']}'; echo $str; ?>