47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?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();
|
|
}
|
|
} |