functions and classes added.

This commit is contained in:
2025-03-20 18:27:17 -07:00
parent 0c41ca9b65
commit 28513d367d
8 changed files with 250 additions and 72 deletions

View File

@@ -2,71 +2,74 @@
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);
$this->conn = new mysqli($dbinfo['host'], $dbinfo['user'], $dbinfo['pass'], $dbinfo['name']);
if ($this->conn->connect_error) {
die("Connection failed: " . $this->conn->connect_error);
}
}
public function query($query) {
public function query($query, $params = []) {
// Prepare the SQL query
if ($stmt = $this->conn->prepare($query)) {
// Bind parameters to the prepared statement (if any)
if (!empty($params)) {
$types = str_repeat('s', count($params)); // Assuming all params are strings
$stmt->bind_param($types, ...$params);
}
// Execute the statement
if (!$stmt->execute()) {
throw new Exception("Query execution failed: " . $stmt->error);
}
// Return the statement result
return $stmt;
} else {
throw new Exception("Query preparation failed: " . $this->conn->error);
}
}
public function getRow($query, $params = []) {
try {
// Perform the query using prepared statement
$stmt = $this->query($query, $params);
// Get the result of the query
$result = $stmt->get_result();
// Fetch the first row from the result
return $result->fetch_assoc();
} catch (Exception $e) {
// Handle the exception (log it, display a message, etc.)
echo "An error occurred: " . $e->getMessage();
return null;
}
}
public function getRows($query, $params = []) {
$stmt = $this->conn->prepare($query);
if (!$stmt) {
die("Query preparation failed: " . $this->conn->error);
}
// Bind parameters if provided
if (!empty($params)) {
$types = str_repeat('s', count($params)); // Assuming all are strings, adjust as needed
$stmt->bind_param($types, ...$params);
}
$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();
$result = $stmt->get_result(); // Requires MySQL Native Driver (mysqlnd)
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();
if ($result) {
return $result->fetch_all(MYSQLI_ASSOC);
} else {
return [];
}
}
public function close() {