Skip to content
Snippets Groups Projects
Commit e2414ab4 authored by Bitha17's avatar Bitha17
Browse files

chore: remove old db config

parent 27559956
Branches
Tags
No related merge requests found
<?php
$host = 'db';
$dbname = 'tubes1_WBD';
$user = 'root';
$pass = 'your_password';
try {
$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
// Execute SQL statements from init.sql
$sqlInit = file_get_contents(__DIR__ . '/init.sql');
$db->exec($sqlInit);
// echo "Connected successfully and database initialized!<br/>";
} catch (PDOException $e) {
// echo "Connection failed: " . $e->getMessage();
}
?>
<?php
include 'connect.php';
// Make sure $db is defined and not null
if (isset($db)) {
try {
$db->beginTransaction();
// Generate and insert 10000 dummy events with random data
for ($i = 1; $i <= 10000; $i++) {
$eventName = "Event $i";
$eventStock = rand(1, 10);
$eventPrice = rand(10, 100);
$eventDate = date('Y-m-d H:i:s', strtotime("+$i days"));
$eventLocation = "Location $i";
$gambar = 'assets/images/' . ($i % 16 + 1) . '.jpg';
$vid = 'assets/videos/video.mp4';
$sqlEvent = "INSERT INTO events (event_name, event_stock, event_price, event_date, event_location, gambar, vid) VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmtEvent = $db->prepare($sqlEvent);
$stmtEvent->execute([$eventName, $eventStock, $eventPrice, $eventDate, $eventLocation, $gambar, $vid]);
// Generate and insert tickets corresponding to the event's event_stock
for ($j = 1; $j <= $eventStock; $j++) {
$ticketName = "Ticket $j for Event $i";
$eventId = $i; // Get the last inserted event ID
$sqlTicket = "INSERT INTO tickets (ticket_name, event_id) VALUES (?, ?)";
$stmtTicket = $db->prepare($sqlTicket);
$stmtTicket->execute([$ticketName, $eventId]);
}
}
// Insert user data
$userData = [
['John Doe', 'john_doe', 'john@example.com', password_hash('password_1', PASSWORD_DEFAULT), 1],
['Jane Smith', 'jane_smith', 'jane@example.com', password_hash('password_2', PASSWORD_DEFAULT), 0],
['Admin User', 'admin_user', 'admin@example.com', password_hash('password_3', PASSWORD_DEFAULT), 1],
['Alice Johnson', 'alice', 'alice@example.com', password_hash('password_4', PASSWORD_DEFAULT), 0],
['Bob Williams', 'bob', 'bob@example.com', password_hash('password_5', PASSWORD_DEFAULT), 0]
];
$sqlUser = "INSERT INTO users (user_name, username, user_email, user_hashedPass, isAdmin) VALUES (?, ?, ?, ?, ?)";
$stmtUser = $db->prepare($sqlUser);
foreach ($userData as $user) {
$stmtUser->execute($user);
}
// Commit the transaction
$db->commit();
echo "Successfully inserted 10000 dummy events, corresponding tickets, and user data.<br/>";
} catch (PDOException $e) {
$db->rollBack();
echo "Error: " . $e->getMessage();
}
} else {
echo "Error: Database connection not established.";
}
$db = null;
?>
<?php
include 'connect.php';
$dotenvContents = file_get_contents(__DIR__ . '/../.env');
$dotenvLines = explode("\n", $dotenvContents);
foreach ($dotenvLines as $line) {
// Skip empty lines and comments
if (!empty($line) && strpos($line, '=') !== false && strpos($line, '#') !== 0) {
list($name, $value) = explode('=', $line, 2);
$_ENV[$name] = $value;
putenv("$name=$value");
}
}
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'reset.php';
include 'data.php';
$db = null;
// echo "Database initialization successful!";
?>
CREATE DATABASE IF NOT EXISTS tubes1_WBD;
USE tubes1_WBD;
CREATE TABLE IF NOT EXISTS events (
event_id INT AUTO_INCREMENT PRIMARY KEY,
event_name CHAR(255),
event_stock INT,
event_price INT,
event_date DATETIME,
event_location CHAR(255),
gambar VARCHAR(255),
vid VARCHAR(255)
);
CREATE TABLE IF NOT EXISTS tickets (
ticket_id INT AUTO_INCREMENT PRIMARY KEY,
ticket_name CHAR(255),
event_id INT,
FOREIGN KEY (event_id) REFERENCES events(event_id) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS users (
user_ID INT AUTO_INCREMENT PRIMARY KEY,
user_name CHAR(255),
username CHAR(255) UNIQUE,
user_email VARCHAR(255) UNIQUE,
user_hashedPass CHAR(255),
isAdmin BOOLEAN
);
CREATE TABLE IF NOT EXISTS pembelian (
pembelian_id INT AUTO_INCREMENT PRIMARY KEY,
ticket_id INT,
user_id INT,
pembelian_created_time DATETIME,
FOREIGN KEY (ticket_id) REFERENCES tickets(ticket_id),
FOREIGN KEY (user_id) REFERENCES users(user_ID) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS subscription (
creator_id int NOT NULL,
subscriber_id int NOT NULL,
status enum('PENDING','ACCEPTED','REJECTED') NOT NULL DEFAULT 'PENDING',
creator_name char(255) NOT NULL,
PRIMARY KEY (creator_id, subscriber_id),
FOREIGN KEY (subscriber_id) REFERENCES users(user_ID) ON DELETE CASCADE
);
\ No newline at end of file
<?php
include 'connect.php';
try {
$db->exec("DROP TABLE IF EXISTS pembelian");
$db->exec("DROP TABLE IF EXISTS tickets");
$db->exec("DROP TABLE IF EXISTS events");
$db->exec("DROP TABLE IF EXISTS users");
// echo "Successfully reset the database<br/>";
} catch (PDOException $e) {
// echo "Error: " . $e->getMessage();
}
$db = null;
?>
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment