Initial commit
This commit is contained in:
47
engine/class.database.php
Normal file
47
engine/class.database.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
defined('ROOT_DIR') || exit;
|
||||
|
||||
class DB {
|
||||
public string $db_host;
|
||||
public string $db_name;
|
||||
public string $db_user;
|
||||
public string $db_password;
|
||||
|
||||
private PDO $dbh;
|
||||
|
||||
public function __construct($db_host, $db_name, $db_user, $db_password)
|
||||
{
|
||||
$this->db_host = $db_host;
|
||||
$this->db_name = $db_name;
|
||||
$this->db_user = $db_user;
|
||||
$this->db_password = $db_password;
|
||||
}
|
||||
|
||||
public function connect(): DB
|
||||
{
|
||||
$this->dbh = new PDO("mysql:host=". $this->db_host. ";dbname=" . $this->db_name . ";charset=utf8mb4", $this->db_user, $this->db_password);
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Запрос на получение информации из базы
|
||||
public function query($query, $params)
|
||||
{
|
||||
log_message("Начало запроса");
|
||||
log_message($query);
|
||||
log_message(json_encode($params));
|
||||
log_message("Конец запроса");
|
||||
|
||||
$sth = $this->dbh->prepare($query);
|
||||
$sth->execute($params);
|
||||
return $sth->fetchAll();
|
||||
}
|
||||
|
||||
// Запрос на сохранение данных в базу
|
||||
public function insert($query, $params)
|
||||
{
|
||||
$sth = $this->dbh->prepare($query);
|
||||
$sth->execute($params);
|
||||
return $this->dbh->lastInsertId();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user