選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

50 行
1000B

  1. <?php
  2. declare(strict_types=1);
  3. namespace Core;
  4. use PDO;
  5. class Database
  6. {
  7. protected PDO $pdo;
  8. public function __construct(array $config)
  9. {
  10. $this->pdo = new PDO(
  11. $config['dsn'],
  12. $config['username'] ?? null,
  13. $config['password'] ?? null,
  14. $config['options'] ?? []
  15. );
  16. }
  17. public function pdo(): PDO
  18. {
  19. return $this->pdo;
  20. }
  21. public function query(string $sql, array $parameters = []): array
  22. {
  23. $statement = $this->pdo->prepare($sql);
  24. $statement->execute($parameters);
  25. return $statement->fetchAll(PDO::FETCH_ASSOC);
  26. }
  27. public function first(string $sql, array $parameters = []): ?array
  28. {
  29. $rows = $this->query($sql, $parameters);
  30. return $rows[0] ?? null;
  31. }
  32. public function execute(string $sql, array $parameters = []): bool
  33. {
  34. $statement = $this->pdo->prepare($sql);
  35. return $statement->execute($parameters);
  36. }
  37. }

Powered by TurnKey Linux.