-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
52 lines (40 loc) · 1.17 KB
/
Database.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
<?php
require_once("Driver.php");
require_once("Table.php");
class Database {
public $DEFAULT_CHARSET = 'utf8';
/** The PDO database connection object. */
var $pdo;
public function __construct ($dbname, $driver=Driver::MySQL, $host = 'localhost', $username = 'root', $password = '') {
$dsn = "$driver:host=$host;dbname=$dbname;charset=$this->DEFAULT_CHARSET";
try {
$this->pdo = new PDO ($dsn, $username, $password);
}
catch (PDOException $e) {
// Unknown database
if ($e->getCode() == 1049) {
die ($e->getMessage());
}
else {
die ($e->getMessage());
}
}
}
public function table ($table) {
if ($this->tableExists($table)) {
return new Table($this, $table);
}
return NULL;
}
private function tableExists($table) {
try {
$result = $this->pdo->query("SELECT 1 FROM $table LIMIT 1");
} catch (Exception $e) {
return FALSE;
}
return $result !== FALSE;
}
public function getPDO () {
return $this->pdo;
}
}