sveden-parser/app/library/Database.php

44 lines
1.2 KiB
PHP

<?php
namespace App\Library;
use PDOException;
use PDO;
class Database extends PDO
{
public function __construct(string $dsn, string $username, string $password)
{
try {
$dsn .= ";charset=utf8";
parent::__construct(
$dsn,
$username,
$password,
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
echo "Подлючено успешно!\n";
} catch (PDOException $e) {
echo "Ошибка подключения:". $e->getMessage() . "\n";
}
}
public function __destruct()
{
echo "Подключение прервано!\n";
}
public function executeQuery(string $sql, array $params) : array|bool
{
try {
$stmt = $this->prepare($sql);
for ($i = 0; $i < count($params); $i++) {
$stmt->bindParam(":v".$i+1, $params[$i]);
}
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Ошибка запроса: " . $e->getMessage() . "\n";
} finally {
return $array;
}
}
}