Files
decdata/views/createView.php
T
2026-06-30 21:34:42 -04:00

69 lines
1.5 KiB
PHP

<style>
body {
background-color: #333333;
color: white;
}
</style>
<?php
// Simple Script to create a MySQL View.
define("INITIALIZATION_FILE_NAME", "../xml/settings.xml");
// Retrieve the View Name.
$view = $_GET["view"] ?? "";
if (empty($view)) {
echo "No view name given. View not created.";
exit();
}
if ((include "{$view}.php") != true) {
echo "{$view}.php not found. View 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.
$host = (string) $applicationSettings->DatabaseServer;
$database = (string) $applicationSettings->DatabaseName;
$user = (string) $applicationSettings->DatabaseUser;
$password = (string) $applicationSettings->DatabasePassword;
try {
$connection = new PDO("mysql:host={$host};dbname={$database}", $user, $password);
} catch (PDOException | Exception $e) {
echo "Unable to connect to the database. <br/>";
echo "Error is: {$e->getMessage()}";
exit();
}
// Create the view.
$connection->exec("drop view if exists {$view}");
try {
$connection->exec($SQL);
} catch (Exception $e) {
echo "Error creating view. <br/>";
echo "Error is: {$e->getMessage()}";
exit();
}
if ($connection->errorCode() != 0) {
echo "Error creating view. <br/>";
echo "<pre>";
print_r($connection->errorInfo());
} else {
echo "View {$view} was created.";
}
?>