1
0
www.mikescher.com/www/internals/modules/database.php

94 lines
1.9 KiB
PHP
Raw Normal View History

2020-01-14 22:25:43 +01:00
<?php
2017-11-09 17:43:34 +01:00
class Database
{
2020-01-15 00:01:20 +01:00
/* @var PDO $pdo */
private $pdo = NULL;
2017-11-09 17:43:34 +01:00
2020-01-15 00:01:20 +01:00
public function __construct(Website $site)
2017-11-09 17:43:34 +01:00
{
2020-01-15 00:29:45 +01:00
$this->connect($site->config);
}
private function connect(array $config)
{
$dsn = "mysql:host=" . $config['host'] . ";dbname=" . $config['database'] . ";charset=utf8";
2020-01-15 00:01:20 +01:00
$opt =
[
2017-11-09 17:43:34 +01:00
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
2020-01-15 00:29:45 +01:00
$this->pdo = new PDO($dsn, $config['user'], $config['password'], $opt);
}
2020-01-15 00:01:20 +01:00
public function sql_query_num($query)
2017-11-09 17:43:34 +01:00
{
2020-01-15 00:29:45 +01:00
return $this->pdo->query($query)->fetch(PDO::FETCH_NUM)[0];
2017-11-09 17:43:34 +01:00
}
2020-01-15 00:01:20 +01:00
public function sql_query_num_prep($query, $params)
2017-11-09 17:43:34 +01:00
{
2020-01-15 00:01:20 +01:00
$stmt = $this->pdo->prepare($query);
2017-11-09 17:43:34 +01:00
foreach ($params as $p)
{
if (strpos($query, $p[0]) !== FALSE) $stmt->bindValue($p[0], $p[1], $p[2]);
}
$stmt->execute();
2020-01-15 00:29:45 +01:00
return $stmt->fetch(PDO::FETCH_NUM)[0];
2017-11-09 17:43:34 +01:00
}
2020-01-15 00:01:20 +01:00
public function sql_query_assoc($query)
2017-11-09 17:43:34 +01:00
{
2020-01-15 00:29:45 +01:00
return $this->pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
2017-11-09 17:43:34 +01:00
}
2020-01-15 00:01:20 +01:00
public function sql_query_assoc_prep($query, $params)
2017-11-09 17:43:34 +01:00
{
2020-01-15 00:01:20 +01:00
$stmt = $this->pdo->prepare($query);
2017-11-09 17:43:34 +01:00
foreach ($params as $p)
{
if (strpos($query, $p[0]) !== FALSE) $stmt->bindValue($p[0], $p[1], $p[2]);
}
$stmt->execute();
2020-01-15 00:01:20 +01:00
return $stmt->fetchAll(PDO::FETCH_ASSOC);
2017-11-09 17:43:34 +01:00
}
2020-01-15 00:01:20 +01:00
public function sql_query_single($query)
2017-11-09 17:43:34 +01:00
{
2020-01-15 00:01:20 +01:00
return $this->pdo->query($query)->fetch(PDO::FETCH_ASSOC);
2017-11-09 17:43:34 +01:00
}
2020-01-15 00:01:20 +01:00
public function sql_query_single_prep($query, $params)
2017-11-09 17:43:34 +01:00
{
2020-01-15 00:01:20 +01:00
$stmt = $this->pdo->prepare($query);
2017-11-09 17:43:34 +01:00
foreach ($params as $p)
{
if (strpos($query, $p[0]) !== FALSE) $stmt->bindValue($p[0], $p[1], $p[2]);
}
$stmt->execute();
2020-01-15 00:01:20 +01:00
return $stmt->fetch(PDO::FETCH_ASSOC);
2017-11-09 17:43:34 +01:00
}
2020-01-15 00:01:20 +01:00
public function sql_exec_prep($query, $params)
2017-11-09 17:43:34 +01:00
{
2020-01-15 00:01:20 +01:00
$stmt = $this->pdo->prepare($query);
2017-11-09 17:43:34 +01:00
foreach ($params as $p)
{
if (strpos($query, $p[0]) !== FALSE) $stmt->bindValue($p[0], $p[1], $p[2]);
}
$stmt->execute();
return $stmt->rowCount();
}
}