File size: 1,016 Bytes
53f6e0f
 
4c38c62
53f6e0f
 
4c38c62
 
 
 
 
 
 
 
53f6e0f
4c38c62
 
 
 
53f6e0f
 
 
 
 
 
4c38c62
53f6e0f
4c38c62
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
/**
 * 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.");
}