Spaces:
Running
Running
| /** | |
| * Database Configuration using Hugging Face Secrets | |
| */ | |
| // 1. Map Hugging Face Secrets to PHP Variables | |
| // The names inside getenv() must match the names you typed in the "Secrets" settings tab. | |
| $host = getenv('DB_HOST'); | |
| $port = getenv('DB_PORT') ?: '3306'; // Default to 3306 if not set | |
| $db = getenv('DB_NAME'); | |
| $user = getenv('DB_USER'); | |
| $pass = getenv('DB_PASS'); | |
| $charset = 'utf8mb4'; | |
| // 2. Set up the DSN (Data Source Name) | |
| $dsn = "mysql:host=$host;port=$port;dbname=$db;charset=$charset"; | |
| // 3. PDO Options for security and error handling | |
| $options = [ | |
| PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
| PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, | |
| PDO::ATTR_EMULATE_PREPARES => false, | |
| ]; | |
| // 4. Establish the connection | |
| try { | |
| $pdo = new PDO($dsn, $user, $pass, $options); | |
| } catch (\PDOException $e) { | |
| // Log error and stop execution | |
| error_log($e->getMessage()); | |
| die("Error: Could not connect to the database. Check Hugging Face Logs."); | |
| } |