From 279be0402dfa61597f409359cc139c2b2cd14f66 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 1 Apr 2021 09:13:10 +0300 Subject: Updated DB to use PDO instead of mysqli --- src/models/DB.php | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) (limited to 'src/models/DB.php') diff --git a/src/models/DB.php b/src/models/DB.php index 1ca0137..77e08a5 100644 --- a/src/models/DB.php +++ b/src/models/DB.php @@ -1,6 +1,4 @@ connection = mysqli_connect(self::$servername, self::$username, self::$password); - - if (!$this->connection) { - die(mysqli_connect_error()); - } + $this->connection = new PDO("mysql:host=" . self::$servername, self::$username, self::$password); + $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Make sure the actual db exists $createDBQuery = "CREATE DATABASE IF NOT EXISTS " . self::$dbname . ";"; - if (!mysqli_query($this->connection, $createDBQuery)) { - die(mysqli_error($this->connection)); - } + $this->connection->exec($createDBQuery); // Make the actual connection to the database - mysqli_select_db($this->connection, self::$dbname); - - $this->createTables(); - } - - private function createTables() { - $this->createTable(JobOffer::TABLE_NAME, JobOffer::TABLE_COLUMNS); - } - - function __destruct() { - mysqli_close($this->connection); + $this->connection->exec("USE " . self::$dbname); } function createTable(string $tableName, array $columns) { @@ -51,8 +34,13 @@ class DB { $query .= "PRIMARY KEY (id));"; - if (!mysqli_query($this->connection, $query)) { - die(mysqli_error($this->connection)); - } + $this->connection->exec($query); + } + + function getAllValues(string $tableName) { + $sth = $this->connection->prepare("SELECT * FROM `{$tableName}`"); + $sth->execute(); + + return $sth->fetchAll(PDO::FETCH_OBJ); } } -- cgit v1.3.1-23-gd82d