Files
atlhousingreport/tables/_createTable.php
T

78 lines
1.8 KiB
PHP
Raw Normal View History

<style>
body {
background-color: #333333;
color: white;
}
</style>
<?php
// Simple Script to create a MySQL Table.
define("INITIALIZATION_FILE_NAME", "../xml/settings.xml");
// Retrieve the Table Name.
$table = (isset($_GET["table"])) ? $_GET["table"] : "";
if (empty($table)) {
echo "No table name given. Table not created.";
exit();
}
if ((include "{$table}.php") != true) {
echo "{$table}.php not found. Table not created.";
exit;
}
// Retrieve Application Settings
$applicationSettings = simplexml_load_file(INITIALIZATION_FILE_NAME);
if (empty($applicationSettings)) {
echo "Unable to load Application Settings." . PHP_EOL;
exit();
}
// Connect to the Database.
if ($applicationSettings->Environment == 'Production') {
$host = 'localhost';
} else {
$host = '192.168.1.190';
}
$database = (string) $applicationSettings->DatabaseName;
$user = (string) $applicationSettings->DatabaseUser;
$password = (string) $applicationSettings->DatabasePassword;
try {
$connection = new PDO("mysql:host={$host};dbname={$database}", $user, $password);
$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException | Exception $e) {
echo "Unable to connect to the database. <br/>";
echo "Error is: {$e->getMessage()}";
exit();
}
// Create the table.
$connection->exec("set foreign_key_checks = 0");
$connection->exec("drop table if exists {$table}");
try {
$connection->exec($SQL);
} catch (PDOException | Exception $e) {
echo "Error creating table. <br/>";
echo "Error is: {$e->getMessage()}";
exit();
}
if ($connection->errorCode() != 0) {
echo "Error creating table. <br/>";
echo "<pre>";
print_r($connection->errorInfo());
} else {
echo "Table {$table} was created.";
}
?>