-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
73 lines (63 loc) · 2.01 KB
/
db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
class Db {
/* singleton */
private static $db;
/* @var $conn PDO */
private $conn=null;
private $stmt;
private static $queries = array();
private static $config = array();
private function __construct(){
try {
if(empty(self::$config))
{
echo 'Database configuration error (config via Db::config(dbName,user,password,host)';
die();
}
$this->conn = new PDO("mysql:host=".self::$config['host'], self::$config['user'], self::$config['password']);
$this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db_name = "`".str_replace("`","``",self::$config['dbName'])."`";
$this->conn->exec("CREATE DATABASE IF NOT EXISTS $db_name");
$this->conn->exec("use $db_name");
}
catch(PDOException $e)
{
$this->conn = null;
echo 'ERROR: ' . $e->getMessage();
die();
}
}
function query($prepare,$execute=null){
if($this->conn==null) return;
self::$queries[]=array($prepare,$execute);
$this->stmt = $this->conn->prepare($prepare);
if ($execute == null)$this->stmt->execute();
else $this->stmt->execute($execute);
return $this->stmt;
}
function queryAndReturnId($prepare,$execute=null)
{
$this->query($prepare, $execute);
return $this->conn->lastInsertId();
}
static function config($dbName,$user,$password,$host='localhost')
{
self::$config = array(
'dbName' => $dbName,
'user' => $user,
'password' => $password,
'host' => $host
);
}
static function getDb()
{
if(empty(self::$db))
self::$db = new Db();
return self::$db;
}
static function getQueries()
{
return self::$queries;
}
}