2021-01-28 15:57:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace Battles\Database;
|
|
|
|
const DATABASE_HOST = '192.168.20.5';
|
|
|
|
const DATABASE_NAME = 'battles';
|
|
|
|
const DATABASE_USER = 'battles';
|
|
|
|
const DATABASE_PASS = 'bottle-neck-horse';
|
|
|
|
const DATABASE_PORT = '32101';
|
|
|
|
use PDO, PDOException;
|
|
|
|
class DBPDO
|
|
|
|
{
|
|
|
|
|
|
|
|
public $pdo;
|
2021-01-28 21:05:34 +00:00
|
|
|
private static $_instance = null;
|
2021-01-28 15:57:55 +00:00
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
}
|
|
|
|
|
2021-01-28 21:05:34 +00:00
|
|
|
public static function INIT(): DBPDO
|
|
|
|
{
|
|
|
|
if (!self::$_instance) {
|
|
|
|
self::$_instance = new DBPDO();
|
|
|
|
}
|
|
|
|
return self::$_instance;
|
|
|
|
}
|
2021-01-28 15:57:55 +00:00
|
|
|
|
|
|
|
function prep_query($query)
|
|
|
|
{
|
|
|
|
return $this->pdo->prepare($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function connect():bool
|
|
|
|
{
|
|
|
|
if (!$this->pdo) {
|
|
|
|
|
|
|
|
$dsn = 'mysql:dbname=' . DATABASE_NAME . ';host=' . DATABASE_HOST . ';port=' . DATABASE_PORT . ';charset=utf8;';
|
|
|
|
$user = DATABASE_USER;
|
|
|
|
$password = DATABASE_PASS;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
|
|
|
|
return true;
|
|
|
|
} catch (PDOException $e) {
|
2021-02-01 14:39:48 +00:00
|
|
|
die($e->getMessage());
|
2021-01-28 15:57:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function table_exists($table_name)
|
|
|
|
{
|
|
|
|
$stmt = $this->prep_query('SHOW TABLES LIKE ?');
|
|
|
|
$stmt->execute(array($table_name));
|
|
|
|
return $stmt->rowCount() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function execute($query, $values = null)
|
|
|
|
{
|
|
|
|
if ($values == null) {
|
|
|
|
$values = array();
|
|
|
|
} else if (!is_array($values)) {
|
|
|
|
$values = array($values);
|
|
|
|
}
|
|
|
|
$stmt = $this->prep_query($query);
|
|
|
|
$stmt->execute($values);
|
|
|
|
return $stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetch($query, $values = null)
|
|
|
|
{
|
|
|
|
if ($values == null) {
|
|
|
|
$values = array();
|
|
|
|
} else if (!is_array($values)) {
|
|
|
|
$values = array($values);
|
|
|
|
}
|
|
|
|
$stmt = $this->execute($query, $values);
|
|
|
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchAll($query, $values = null, $key = null)
|
|
|
|
{
|
|
|
|
if ($values == null) {
|
|
|
|
$values = array();
|
|
|
|
} else if (!is_array($values)) {
|
|
|
|
$values = array($values);
|
|
|
|
}
|
|
|
|
$stmt = $this->execute($query, $values);
|
|
|
|
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
// Allows the user to retrieve results using a
|
|
|
|
// column from the results as a key for the array
|
|
|
|
if ($key != null && $results[0][$key]) {
|
|
|
|
$keyed_results = array();
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$keyed_results[$result[$key]] = $result;
|
|
|
|
}
|
|
|
|
$results = $keyed_results;
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2021-01-28 21:05:34 +00:00
|
|
|
function ofetch($query, $values = null)
|
|
|
|
{
|
|
|
|
if ($values == null) {
|
|
|
|
$values = array();
|
|
|
|
} else if (!is_array($values)) {
|
|
|
|
$values = array($values);
|
|
|
|
}
|
|
|
|
$stmt = $this->execute($query, $values);
|
|
|
|
return $stmt->fetch(PDO::FETCH_OBJ);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ofetchAll($query, $values = null, $key = null)
|
|
|
|
{
|
|
|
|
if ($values == null) {
|
|
|
|
$values = array();
|
|
|
|
} else if (!is_array($values)) {
|
|
|
|
$values = array($values);
|
|
|
|
}
|
|
|
|
$stmt = $this->execute($query, $values);
|
|
|
|
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
|
|
|
|
|
|
|
|
// Allows the user to retrieve results using a
|
|
|
|
// column from the results as a key for the array
|
|
|
|
if ($key != null && $results[0][$key]) {
|
2021-01-31 23:43:48 +00:00
|
|
|
$keyed_results = (object) array();
|
2021-01-28 21:05:34 +00:00
|
|
|
foreach ($results as $result) {
|
2021-01-31 23:43:48 +00:00
|
|
|
$keyed_results->$result[$key] = $result;
|
2021-01-28 21:05:34 +00:00
|
|
|
}
|
|
|
|
$results = $keyed_results;
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:57:55 +00:00
|
|
|
function lastInsertId()
|
|
|
|
{
|
|
|
|
return $this->pdo->lastInsertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|