first commit
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* Database Configuration
|
||||
* Production fitness tracking app database connection
|
||||
*/
|
||||
|
||||
class Database {
|
||||
private static $instance = null;
|
||||
private $connection;
|
||||
|
||||
private $db_host = 'localhost';
|
||||
private $db_user = 'fitness_app_user';
|
||||
private $db_pass = 'your_secure_password_here';
|
||||
private $db_name = 'fitness_app';
|
||||
private $db_port = 3306;
|
||||
|
||||
private function __construct() {
|
||||
try {
|
||||
$this->connection = new mysqli(
|
||||
$this->db_host,
|
||||
$this->db_user,
|
||||
$this->db_pass,
|
||||
$this->db_name,
|
||||
$this->db_port
|
||||
);
|
||||
|
||||
// Check connection
|
||||
if ($this->connection->connect_error) {
|
||||
throw new Exception('Database connection failed: ' . $this->connection->connect_error);
|
||||
}
|
||||
|
||||
// Set charset
|
||||
$this->connection->set_charset('utf8mb4');
|
||||
|
||||
// Enable error reporting
|
||||
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log('Database Error: ' . $e->getMessage());
|
||||
http_response_code(500);
|
||||
die(json_encode(['error' => 'Database connection failed']));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Singleton instance getter
|
||||
*/
|
||||
public static function getInstance() {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get connection
|
||||
*/
|
||||
public function getConnection() {
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and execute query
|
||||
*/
|
||||
public function prepare($query) {
|
||||
return $this->connection->prepare($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape string
|
||||
*/
|
||||
public function escape($string) {
|
||||
return $this->connection->real_escape_string($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close connection
|
||||
*/
|
||||
public function close() {
|
||||
if ($this->connection) {
|
||||
$this->connection->close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Begin transaction
|
||||
*/
|
||||
public function beginTransaction() {
|
||||
$this->connection->begin_transaction();
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit transaction
|
||||
*/
|
||||
public function commit() {
|
||||
$this->connection->commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rollback transaction
|
||||
*/
|
||||
public function rollback() {
|
||||
$this->connection->rollback();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last inserted ID
|
||||
*/
|
||||
public function getLastInsertId() {
|
||||
return $this->connection->insert_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get affected rows
|
||||
*/
|
||||
public function getAffectedRows() {
|
||||
return $this->connection->affected_rows;
|
||||
}
|
||||
}
|
||||
|
||||
// Prevent cloning
|
||||
final class SingletonDatabase extends Database {
|
||||
private function __clone() {}
|
||||
private function __wakeup() {}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user