|
@@ -7,7 +7,8 @@ class Database {
|
|
|
public $conn;
|
|
public $conn;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
public function __construct() {
|
|
|
- // Load local environment file for development
|
|
|
|
|
|
|
+ // In Docker, environment variables are passed directly
|
|
|
|
|
+ // For local development, try to load .env.local file
|
|
|
$envFile = __DIR__ . '/../.env.local';
|
|
$envFile = __DIR__ . '/../.env.local';
|
|
|
if (file_exists($envFile)) {
|
|
if (file_exists($envFile)) {
|
|
|
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
@@ -19,23 +20,40 @@ class Database {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $this->host = getenv('DB_HOST') ?: 'localhost';
|
|
|
|
|
- $this->db_name = getenv('DB_NAME') ?: 'inventory_db';
|
|
|
|
|
- $this->username = getenv('DB_USER') ?: 'root';
|
|
|
|
|
- $this->password = getenv('DB_PASS') ?: '';
|
|
|
|
|
|
|
+ // Get database configuration from environment variables
|
|
|
|
|
+ $this->host = getenv('DB_HOST');
|
|
|
|
|
+ $this->db_name = getenv('DB_NAME');
|
|
|
|
|
+ $this->username = getenv('DB_USER');
|
|
|
|
|
+ $this->password = getenv('DB_PASS');
|
|
|
|
|
+
|
|
|
|
|
+ // Debug: Log environment variables (remove in production)
|
|
|
|
|
+ error_log("Database config - Host: " . ($this->host ?: 'NOT SET') .
|
|
|
|
|
+ ", DB: " . ($this->db_name ?: 'NOT SET') .
|
|
|
|
|
+ ", User: " . ($this->username ?: 'NOT SET'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function getConnection() {
|
|
public function getConnection() {
|
|
|
$this->conn = null;
|
|
$this->conn = null;
|
|
|
|
|
|
|
|
|
|
+ // Validate required database configuration
|
|
|
|
|
+ if (empty($this->host) || empty($this->db_name) || empty($this->username)) {
|
|
|
|
|
+ error_log("Database configuration missing: Host=" . ($this->host ?: 'MISSING') .
|
|
|
|
|
+ ", DB=" . ($this->db_name ?: 'MISSING') .
|
|
|
|
|
+ ", User=" . ($this->username ?: 'MISSING'));
|
|
|
|
|
+ throw new Exception("Database configuration is incomplete. Please check environment variables.");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
$dsn = "mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4";
|
|
$dsn = "mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4";
|
|
|
|
|
+ error_log("Attempting database connection: $dsn with user " . $this->username);
|
|
|
$this->conn = new PDO($dsn, $this->username, $this->password);
|
|
$this->conn = new PDO($dsn, $this->username, $this->password);
|
|
|
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
$this->conn->exec("set names utf8");
|
|
$this->conn->exec("set names utf8");
|
|
|
|
|
+ error_log("Database connection successful");
|
|
|
} catch(PDOException $exception) {
|
|
} catch(PDOException $exception) {
|
|
|
error_log("Database connection error: " . $exception->getMessage());
|
|
error_log("Database connection error: " . $exception->getMessage());
|
|
|
- throw new Exception("Database connection failed");
|
|
|
|
|
|
|
+ error_log("Connection details - Host: " . $this->host . ", DB: " . $this->db_name . ", User: " . $this->username);
|
|
|
|
|
+ throw new Exception("Database connection failed: " . $exception->getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $this->conn;
|
|
return $this->conn;
|