Files
huntingclubroster/views/createView.php
T
2026-10-18 19:27:17 -04:00

64 lines
1.6 KiB
PHP

<?php
if (PHP_SAPI !== 'cli') { http_response_code(404); exit; }
// Simple Script to create a MySQL View.
define("INITIALIZATION_FILE_NAME", getenv("HCR_SETTINGS_FILE") ?: (__DIR__ . "/../xml/" . (is_file(__DIR__ . "/../xml/settings.local.xml") ? "settings.local.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.";
}
?>