novaconium/src/Database.php
2024-09-06 12:45:37 -07:00

75 lines
1.7 KiB
PHP

<?php
class Database {
private $host;
private $user;
private $pass;
private $dbname;
private $conn;
public function __construct($dbinfo) {
$this->host = $dbinfo['host'];
$this->user = $dbinfo['user'];
$this->pass = $dbinfo['pass'];
$this->dbname = $dbinfo['name'];
$this->connect();
}
private function connect() {
$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
if ($this->conn->connect_error) {
die("Connection failed: " . $this->conn->connect_error);
}
}
public function query($query) {
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt->get_result();
}
public function getRow($query) {
$result = $this->query($query);
return $result->fetch_assoc();
}
public function debugGetRow($query) {
echo "<h1>Debug GetRow Query</h1>";
echo "<div class='debug-query'>Query: $query</div>";
$result = $this->query($query);
$row = $result->fetch_assoc();
echo "<pre>";
print_r($row);
echo "</pre>";
die();
}
public function getRows($query) {
$result = $this->query($query);
return $result->fetch_all(MYSQLI_ASSOC);
}
public function debugGetRows($query) {
echo "<h1>Debug GetRows Query</h1>";
echo "<div class='debug-query'>Query: $query</div>";
$result = $this->query($query);
$rows = $result->fetch_all(MYSQLI_ASSOC);
echo "<pre>";
print_r($rows);
echo "</pre>";
die();
}
public function close() {
$this->conn->close();
}
}