mirror of
https://github.com/Neetpone/ponepaste.git
synced 2025-03-12 14:40:09 +01:00
44 lines
1.2 KiB
PHP
44 lines
1.2 KiB
PHP
<?php
|
|
|
|
class DatabaseHandle {
|
|
private PDO $conn;
|
|
|
|
public function __construct(string $conString, string $username, string $password) {
|
|
$this->conn = new PDO($conString, $username, $password, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
PDO::ATTR_EMULATE_PREPARES => false
|
|
]);
|
|
}
|
|
|
|
public function prepare(string $query) : PDOStatement {
|
|
return $this->conn->prepare($query);
|
|
}
|
|
|
|
public function query(string $query, array $params = null) : PDOStatement {
|
|
if (empty($params)) {
|
|
return $this->conn->query($query);
|
|
}
|
|
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->execute($params);
|
|
|
|
return $stmt;
|
|
}
|
|
|
|
public function querySelectOne(string $query, array $params = null) : array|null {
|
|
$stmt = $this->query($query, $params);
|
|
|
|
if ($row = $stmt->fetch()) {
|
|
return $row;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function queryInsert(string $query, array $params = null) : int {
|
|
$this->query($query, $params);
|
|
|
|
return (int)$this->conn->lastInsertId();
|
|
}
|
|
}
|