sveden-parser/app/library/Database.php

64 lines
1.9 KiB
PHP

<?php
namespace App\Library;
use PDO;
use PDOException;
class Database extends PDO
{
public function __construct($dsn, $username, $password)
{
try {
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 insert(string $table, array $data) : void
{
$stmt = $this->prepare("
INSERT INTO sveden_table_education
(spec_code, spec_name, edu_forms, edu_level, contingent)
VALUES
(:spec_code, :spec_name, :edu_forms, :edu_level, :contingent)"
);
foreach ($data as $row) {
try {
$stmt->bindParam(':spec_code', $row['eduCode']);
$stmt->bindParam(':spec_name', $row['eduName']);
$stmt->bindParam(':edu_forms', $row['edoForms']);
$stmt->bindParam(':edu_level', $row['eduLevel']);
$stmt->bindParam(':contingent', $row['contingent']);
$stmt->execute();
} catch (PDOException $e) {
echo "Ошибка запроса: " . $e->getMessage() . "\n";
}
}
}
public function select(string $table) : array
{
$stmt = $this->prepare("SELECT * FROM $table");
try {
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Ошибка запроса: " . $e->getMessage() . "\n";
} finally {
return $array;
}
}
}