.csv $host = getenv('DB_HOST') ?: '127.0.0.1'; $user = getenv('DB_USER') ?: 'root'; $pass = getenv('DB_PASS') ?: ''; $srcDbName = getenv('DB_OLD_NAME') ?: 'tippspiel_legacy'; $out = $argv[1] ?? (__DIR__ . '/export_kontodaten_' . date('Ymd_His') . '.csv'); try { $pdo = new PDO("mysql:host=$host;charset=utf8mb4", $user, $pass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Aktuelle Saison ermitteln $currentId = (int)$pdo->query( "SELECT current_season_id FROM `$srcDbName`.`settings` ORDER BY id LIMIT 1" )->fetchColumn(); if ($currentId <= 0) { die("Fehler: Keine aktive Saison in der Quell-DB '$srcDbName' gefunden.\n"); } // Letzte Saison = höchste ID unterhalb der aktuellen $stmt = $pdo->prepare("SELECT MAX(id) FROM `$srcDbName`.`seasons` WHERE id < ?"); $stmt->execute([$currentId]); $lastId = (int)$stmt->fetchColumn(); $seasonIds = array_values(array_unique(array_filter([$currentId, $lastId]))); $placeholders = implode(',', array_fill(0, count($seasonIds), '?')); // Alle User, die in einer der beiden Saisons bezahlt haben (paid_date gesetzt) $sql = "SELECT u.id AS user_id, u.name, u.email, u.iban, u.bankname, u.blz, u.kontonr, u.mobile, p.season_id FROM `$srcDbName`.`users` u JOIN `$srcDbName`.`payments` p ON p.user_id = u.id AND p.paid_date IS NOT NULL WHERE p.season_id IN ($placeholders) ORDER BY u.id, p.season_id"; $stmt = $pdo->prepare($sql); $stmt->execute($seasonIds); $users = []; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $id = (int)$row['user_id']; if (!isset($users[$id])) { $users[$id] = [ 'user_id' => $id, 'name' => (string)$row['name'], 'email' => (string)$row['email'], 'iban' => (string)$row['iban'], 'bankname' => $row['bankname'] !== null ? (string)$row['bankname'] : '', 'blz' => $row['blz'] !== null ? (string)$row['blz'] : '', 'kontonr' => $row['kontonr'] !== null ? (string)$row['kontonr'] : '', 'mobile' => $row['mobile'] !== null ? (string)$row['mobile'] : '', 'seasons' => [], ]; } $users[$id]['seasons'][] = (int)$row['season_id']; } $fh = fopen($out, 'w'); if ($fh === false) { die("Fehler: Ausgabedatei '$out' kann nicht geöffnet werden.\n"); } // UTF-8 BOM, damit Umlaute in Excel korrekt angezeigt werden fwrite($fh, "\xEF\xBB\xBF"); $header = ['user_id', 'name', 'email', 'iban', 'bankname', 'blz', 'kontonr', 'mobile', 'aktiv_aktuell', 'aktiv_letzte']; fputcsv($fh, $header, ';', '"', '\\'); $count = 0; foreach ($users as $u) { $seasons = array_unique($u['seasons']); fputcsv($fh, [ $u['user_id'], $u['name'], $u['email'], $u['iban'], $u['bankname'], $u['blz'], $u['kontonr'], $u['mobile'], in_array($currentId, $seasons) ? 1 : 0, in_array($lastId, $seasons) ? 1 : 0, ], ';', '"', '\\'); $count++; } fclose($fh); echo "Export abgeschlossen.\n"; echo "Quell-DB: $srcDbName\n"; echo "Aktuelle Saison: $currentId, letzte Saison: " . ($lastId > 0 ? (string)$lastId : '-') . "\n"; echo "$count Spieler exportiert.\n"; echo "Ausgabedatei: $out\n"; } catch (PDOException $e) { die("Datenbankfehler: " . $e->getMessage() . "\n"); } catch (\Throwable $e) { die("Fehler: " . $e->getMessage() . "\n"); }