diff options
Added JobOffer model and implemented a basic system for creating tables
1 files changed, 27 insertions, 0 deletions
| -rw-r--r-- | src/models/DB.php | 27 |
diff --git a/src/models/DB.php b/src/models/DB.php index f6db512..1ca0137 100644 --- a/src/models/DB.php +++ b/src/models/DB.php @@ -1,10 +1,16 @@ <?php +include_once("JobOffer.php"); + class DB { private static $servername = "localhost"; private static $username = "root"; private static $password = "password"; private static $dbname = "devrix_job_board"; + public const STRING_TYPE = "VARCHAR(100)"; + public const FLOAT_TYPE = "DOUBLE(16,4)"; + public const INTEGER_TYPE = "INT"; + private $connection; function __construct() { @@ -23,9 +29,30 @@ class DB { // 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); } + + function createTable(string $tableName, array $columns) { + $query = "CREATE TABLE IF NOT EXISTS {$tableName}( + id INT NOT NULL AUTO_INCREMENT,"; + + foreach ($columns as $column) { + $query .= "{$column},"; + } + + $query .= "PRIMARY KEY (id));"; + + if (!mysqli_query($this->connection, $query)) { + die(mysqli_error($this->connection)); + } + } } |
