Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database
- {
- // CONFIGS DEFAULT
- private $host = 'localhost';
- private $db = 'test';
- private $user = 'root';
- private $password = 'root';
- private $charset = 'utf8';
- private $options = [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- ];
- // CONFIGS OTHER
- private $dsn;
- public $pdo;
- public $tableName;
- public $id;
- // METHODS
- public function __construct() {
- $this->dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
- $this->pdo = new PDO($this->dsn, $this->user, $this->password, $this->options);
- }
- public function getDsn() {
- return $this->dsn;
- }
- public function getAll() {
- $stmt = $this->pdo->query("SELECT * FROM $this->tableName");
- return $stmt->fetchAll();
- }
- public function getOne() {
- $stmt = $this->pdo->query("SELECT * FROM $this->tableName WHERE id = $this->id");
- return $stmt->fetchAll();
- }
- public function deleteAll() {
- $this->pdo->query("DELETE FROM $this->tableName");
- }
- public function deleteOne() {
- $this->pdo->query("DELETE FROM $this->tableName WHERE id = $this->id");
- }
- public function updateOne($setParams) {
- $this->pdo->query("UPDATE $this->tableName SET $setParams WHERE id = $this->id");
- }
- public function insert($setParams) {
- $this->pdo->query("INSERT INTO $this->tableName SET $setParams");
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement