1
0
Fork 0
forked from CRSS/Website
Website/util/Database.php

60 lines
1.5 KiB
PHP
Raw Normal View History

2023-09-15 21:04:00 +00:00
<?php
class Database {
private string $host;
private string $user;
private string $pass;
private string $db;
private mysqli $conn;
function __construct($host, $user, $pass, $db) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db = $db;
$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->db);
}
function createUserRecord($user): void {
$sql = 'INSERT IGNORE INTO users (id, username, display_name) VALUES (?, ?, ?)';
$stmt = $this->conn->prepare($sql);
$stmt->bind_param('iss', $user['id'], $user['username'], $user['global_name']);
$stmt->execute();
}
2023-09-18 16:10:55 +00:00
function getUserRecordFromUsername($username): array | null {
2023-09-15 21:04:00 +00:00
$sql = 'SELECT * FROM users WHERE username = ?';
$stmt = $this->conn->prepare($sql);
$stmt->bind_param('s', $username);
$stmt->execute();
$res = $stmt->get_result();
if($res->num_rows > 0) {
return $res->fetch_assoc();
} else {
return null;
}
}
2023-09-18 16:10:55 +00:00
function getUserRecordFromId($id): array | null {
2023-09-15 21:04:00 +00:00
$sql = 'SELECT * FROM users WHERE id = ?';
$stmt = $this->conn->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
$res = $stmt->get_result();
if($res->num_rows > 0) {
return $res->fetch_assoc();
} else {
return null;
}
}
}