Files
sam-kd/class/db.class.php
hskwon aca1767eb9 초기 커밋: 5130 레거시 시스템
- URL 하드코딩 → .env APP_URL 기반 동적 URL로 변경
- DB 연결 하드코딩 → .env 기반으로 변경
- MySQL strict mode DATE 오류 수정
2025-12-10 20:14:31 +09:00

37 lines
1012 B
PHP

<?php
// .env 파일 로드
function loadEnvForClass() {
$envFile = $_SERVER['DOCUMENT_ROOT'] . '/.env';
if (file_exists($envFile)) {
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos($line, '=') !== false && strpos($line, '#') !== 0) {
list($key, $value) = explode('=', $line, 2);
$_ENV[trim($key)] = trim($value);
}
}
}
}
class Dbh {
private $host;
private $user;
private $pw;
private $dbname;
public function __construct() {
loadEnvForClass();
$this->host = $_ENV['DB_HOST'] ?? 'localhost';
$this->dbname = $_ENV['DB_NAME'] ?? 'chandj';
$this->user = $_ENV['DB_USER'] ?? 'chandj';
$this->pw = $_ENV['DB_PASS'] ?? '';
}
function connect(){
$dsn = "mysql:host=".$this->host.";dbname=".$this->dbname.";charset=utf8";
$pdo = new PDO($dsn, $this->user, $this->pw);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
}
}
?>