SQL Table -> JSON
query("SELECT * FROM $table");
echo "SQL DATA
";
echo "Formatted
";
echo "Vorname | Nachname | Geburtsdatum | Gehalt | Geschlecht | Einstellungsdatum
";
$baseJson = [
"Table" => $table,
"Database" => $dbname,
"Host" => $host,
"Data" => []
];
// append data to json and show it
while ($row = $dbData->fetch()) {
echo $row["m_vorname"]." | ".
$row["m_nachname"]." | ".
$row["m_geburtsdatum"]." | ".
$row["m_gehalt"]." | ".
$row["m_geschlecht"]." | ".
$row["m_einstellung"]."
";
$baseJson["Data"][] = [
"vorname" => $row["m_vorname"],
"nachname" => $row["m_nachname"],
"geburtsdatum" => $row["m_geburtsdatum"],
"gehalt" => $row["m_gehalt"],
"geschlecht" => $row["m_geschlecht"],
"einstellung" => $row["m_einstellung"]
];
}
echo "JSON Array
";
// transfer php array into json
$json = json_encode($baseJson, JSON_PRETTY_PRINT);
echo "$json
";
// safe file, show error or show written bytes
$file = file_put_contents($jsonFileName, $json);
if ($file === false) {
echo "
Fehler beim Schreiben der Datei.";
} else {
echo "
dbdata.json geschrieben ($file Bytes).";
}
?>