57 lines
1.1 KiB
PHP
57 lines
1.1 KiB
PHP
<?php
|
|
|
|
class PostgresDb
|
|
{
|
|
private $pdo;
|
|
|
|
public function __construct()
|
|
{
|
|
$host = "10.10.123.238";
|
|
$port = "5432";
|
|
$dbname = "simrs";
|
|
$user = "brawijaya";
|
|
$password = "ub*2025";
|
|
|
|
$dsn = "pgsql:host=$host;port=$port;dbname=$dbname";
|
|
|
|
try {
|
|
|
|
$this->pdo = new PDO($dsn, $user, $password, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
|
]);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
die("Database connection failed: " . $e->getMessage());
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public function query($sql, $params = [])
|
|
{
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
|
|
return $stmt;
|
|
}
|
|
|
|
|
|
public function fetchAll($sql, $params = [])
|
|
{
|
|
return $this->query($sql, $params)->fetchAll();
|
|
}
|
|
|
|
|
|
public function fetch($sql, $params = [])
|
|
{
|
|
return $this->query($sql, $params)->fetch();
|
|
}
|
|
|
|
|
|
public function lastInsertId()
|
|
{
|
|
return $this->pdo->lastInsertId();
|
|
}
|
|
} |