<?php
// Include configuration
require_once 'config.php';
// Create connection using configuration constants
$conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD);
// Check connection
if ($conn->connect_error) {
if (!$is_production) {
die("Connection failed: " . $conn->connect_error);
} else {
// In production, log error instead of displaying it
error_log("Database connection failed: " . $conn->connect_error);
die("Database connection error. Please try again later.");
}
}
// Set charset to utf8mb4 for full Arabic support
$conn->set_charset(CHARSET);
// Create database if it doesn't exist (only in development)
if (!$is_production) {
$sql = "CREATE DATABASE IF NOT EXISTS `" . DB_NAME . "` CHARACTER SET " . CHARSET . " COLLATE utf8mb4_general_ci";
if ($conn->query($sql) === TRUE) {
// Database created or already exists
} else {
echo "Error creating database: " . $conn->error;
}
}
// Select the database
$conn->select_db(DB_NAME);
// Function to safely close connection
function closeConnection() {
global $conn;
if ($conn) {
$conn->close();
}
}
// Register shutdown function to close connection
register_shutdown_function('closeConnection');
?>