/home/awneajlw/public_html/codestechvista.com/config/database.php
<?php
// Prevent any output that might cause headers already sent error
if (ob_get_level()) {
ob_clean();
}
class Database {
// For local development (Laragon)
private $host = 'localhost';
private $db_name = 'awneajlw_eyeclinicwebsitenew';
private $username = 'awneajlw_eyeclinicwebsitenew';
private $password = 'eyeclinicwebsitenew';
private $conn;
private static $instance = null;
public function getConnection() {
// Close existing connection if any
$this->conn = null;
try {
// Enhanced connection options for better connection management
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4",
PDO::ATTR_PERSISTENT => false, // Disable persistent connections
PDO::ATTR_TIMEOUT => 10 // Reduced timeout
];
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4",
$this->username, $this->password, $options);
// Set connection to close automatically
$this->conn->setAttribute(PDO::ATTR_PERSISTENT, false);
} catch(PDOException $exception) {
error_log("Database connection error: " . $exception->getMessage());
// Don't echo error in production, just log it
if (strpos($_SERVER['HTTP_HOST'], 'localhost') !== false) {
echo "Connection error: " . $exception->getMessage();
}
$this->conn = null;
}
return $this->conn;
}
// Singleton pattern to prevent multiple connections
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
// Close connection when done
public function closeConnection() {
$this->conn = null;
}
// Destructor to ensure connection is closed
public function __destruct() {
$this->closeConnection();
}
}
?>