Big Update Added Services and Admin
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
class Database {
|
||||
|
||||
private $conn;
|
||||
public $lastid;
|
||||
|
||||
public function __construct($dbinfo) {
|
||||
$this->conn = new mysqli($dbinfo['host'], $dbinfo['user'], $dbinfo['pass'], $dbinfo['name']);
|
||||
@@ -13,43 +14,90 @@ class Database {
|
||||
}
|
||||
|
||||
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);
|
||||
// Clean up pending results to avoid "commands out of sync"
|
||||
while ($this->conn->more_results() && $this->conn->next_result()) {
|
||||
if ($res = $this->conn->use_result()) {
|
||||
$res->free();
|
||||
}
|
||||
|
||||
// Execute the statement
|
||||
if (!$stmt->execute()) {
|
||||
throw new Exception("Query execution failed: " . $stmt->error);
|
||||
}
|
||||
|
||||
// Return the statement result
|
||||
return $stmt;
|
||||
} else {
|
||||
}
|
||||
|
||||
// Prepare the SQL statement
|
||||
$stmt = $this->conn->prepare($query);
|
||||
if (!$stmt) {
|
||||
throw new Exception("Query preparation failed: " . $this->conn->error);
|
||||
}
|
||||
|
||||
// Bind parameters if needed
|
||||
if (!empty($params)) {
|
||||
$types = str_repeat('s', count($params)); // Use 's' for all types, or detect types dynamically
|
||||
$stmt->bind_param($types, ...$params);
|
||||
}
|
||||
|
||||
// Execute the statement
|
||||
if (!$stmt->execute()) {
|
||||
$stmt->close();
|
||||
throw new Exception("Query execution failed: " . $stmt->error);
|
||||
}
|
||||
|
||||
// Save last insert id if it's an INSERT query
|
||||
if (preg_match('/^\s*INSERT/i', $query)) {
|
||||
$this->lastid = $this->conn->insert_id;
|
||||
} else {
|
||||
$this->lastid = 0;
|
||||
}
|
||||
|
||||
// Decide what to return
|
||||
// For SELECT/SHOW etc., return result set
|
||||
if (preg_match('/^\s*(SELECT|SHOW|DESCRIBE|EXPLAIN)/i', $query)) {
|
||||
$result = $stmt->get_result();
|
||||
$stmt->close();
|
||||
return $result;
|
||||
}
|
||||
|
||||
// For INSERT/UPDATE/DELETE, return success status
|
||||
$success = $stmt->affected_rows;
|
||||
$stmt->close();
|
||||
return $success;
|
||||
}
|
||||
|
||||
public function lastid() {
|
||||
return $this->lastid;
|
||||
}
|
||||
|
||||
public function getRow($query, $params = []) {
|
||||
try {
|
||||
// Perform the query using prepared statement
|
||||
$stmt = $this->query($query, $params);
|
||||
|
||||
// Get the result of the query
|
||||
// Prepare the SQL statement
|
||||
$stmt = $this->conn->prepare($query);
|
||||
if (!$stmt) {
|
||||
throw new Exception("Query preparation failed: " . $this->conn->error);
|
||||
}
|
||||
|
||||
// Bind parameters
|
||||
if (!empty($params)) {
|
||||
$types = str_repeat('s', count($params)); // You may improve this with actual type detection
|
||||
$stmt->bind_param($types, ...$params);
|
||||
}
|
||||
|
||||
// Execute the statement
|
||||
if (!$stmt->execute()) {
|
||||
$stmt->close();
|
||||
throw new Exception("Query execution failed: " . $stmt->error);
|
||||
}
|
||||
|
||||
// Get result
|
||||
$result = $stmt->get_result();
|
||||
|
||||
// Fetch the first row from the result
|
||||
return $result->fetch_assoc();
|
||||
$row = $result->fetch_assoc();
|
||||
|
||||
$stmt->close();
|
||||
return $row;
|
||||
|
||||
} 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) {
|
||||
|
||||
Reference in New Issue
Block a user