amdp3-metaforums/Application/Services/Database.php

44 lines
1.5 KiB
PHP
Raw Normal View History

2019-11-18 13:33:45 +00:00
<?php
namespace Application\Services;
use Application\Services\ServiceContainer;
use Application\Foundations\SQLHelper;
class Database {
private $conn;
private $config;
public function __construct() {
$this->config = ServiceContainer::Config();
$this->conn = mysqli_connect($this->config->db_host(),$this->config->db_user(),$this->config->db_pass(),$this->config->db_name());
}
public function insert($table, $data) {
$insert_data = SQLHelper::encode_list($data);
$key_names = array_keys($insert_data);
$query = "INSERT INTO ".$table." (".implode(",",$key_names).") VALUES (".implode(",",$insert_data).")";
$result = mysqli_query($this->conn,$query);
if($result) {
return mysqli_insert_id($this->conn);
} else {
echo mysqli_error($this->conn);
return null;
}
}
public function update($query) {
$result = mysqli_query($this->conn,$query);
return $result;
}
public function select($query) {
$result = mysqli_query($this->conn,$query);
if($result) {
return mysqli_fetch_all($result,MYSQLI_ASSOC);
} else {
2019-11-19 03:36:39 +00:00
return mysqli_error($this->conn);
2019-11-18 13:33:45 +00:00
}
}
// Escaping strings requires DB connection, which is only handled by the Database service.
public function escapeString($str) {
return mysqli_real_escape_string($this->conn,$str);
}
}