You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB

5 months ago
<?php
require_once __DIR__."/dotenv.php";
5 months ago
class MySQLConnection {
private int $port;
private string $host;
private string $user;
private string $pass;
private string $dbName;
5 months ago
private mysqli $mysqli;
public function __construct() {
$this->host = 'localhost';
$this->port = intval($_ENV['MYSQL_PORT']);
$this->user = $_ENV['MYSQL_USER'];
$this->pass = $_ENV['MYSQL_PASSWORD'];
$this->dbName = $_ENV['MYSQL_DATABASE'];
5 months ago
$this->createConn();
}
private function createConn(): void {
$this->mysqli = new mysqli($this->host . ":" . $this->port, $this->user, $this->pass, $this->dbName);
$this->mysqli->set_charset("utf8");
5 months ago
}
public function query($sql): mysqli_result|bool {
return $this->mysqli->query($sql);
}
public function prepare($sql): bool|mysqli_stmt {
return $this->mysqli->prepare($sql);
}
public function changeDB($dbName): void {
$this->dbName = $dbName;
$this->mysqli->select_db($dbName);
}
public function escape(string $string): string{
return $this->mysqli->real_escape_string($string);
}
public function __destruct() {
$this->mysqli->close();
}
}