$query_main = "SELECT k.id, k.name, k.plz, k.stadt, k.bland, r.name, f.name, l.kurz, l.id, SUM(a.dekosetrx) AS sumdekosetrx, SUM(a.dekosette) AS sumdekosette, SUM(a.flyer) AS sumflyer, SUM(a.verkostungrx) AS sumverkostungrx, SUM(a.verkostungte) AS sumverkostungte, SUM(a.posterrx) AS sumposterrx, SUM(a.posterte) AS sumposterte, SUM(a.brief) AS sumbrief, MAX(a.kontakt) AS akontakt
FROM kontakte k
LEFT JOIN auftrag a ON k.id = a.kontaktid
LEFT JOIN reform r ON k.grosshandel = r.id
LEFT JOIN filter f ON k.filter = f.id
LEFT JOIN land l ON k.land = l.id";
$query_group = " GROUP BY k.id";
$query_order = " ORDER BY ".$order." ".$sortierung;
$query_limit = " LIMIT ".$limita.", ".$limitb."";
$query = $query_main.$query_group.$query_order.$query_limit;
$dbpdo = new PDO('mysql:dbname=dbXXXXX;host=XX.XX.XX.XX;port=3304', 'dbXXXXX', 'XXXXXX');
foreach ($dbpdo->query($query) as $entry) {
$d_id = $entry['k.id'];
$d_name = $entry['k.name'];
$d_plz = $entry['k.plz'];
$d_stadt = $entry['k.stadt'];
$f_name = $entry['f.name'];
$l_id = $entry['l.id'];
}