1
|
<?php
|
2
|
|
3
|
require_once "db-exception.php";
|
4
|
|
5
|
class DB_PDO {
|
6
|
|
7
|
protected $user;
|
8
|
protected $pass;
|
9
|
protected $dbhost;
|
10
|
protected $dbname;
|
11
|
protected $dbh;
|
12
|
|
13
|
protected $logs;
|
14
|
|
15
|
public function __construct($user, $pass, $dbhost, $dbname) {
|
16
|
$this->user = $user;
|
17
|
$this->pass = $pass;
|
18
|
$this->dbhost = $dbhost;
|
19
|
$this->dbname = $dbname;
|
20
|
}
|
21
|
|
22
|
protected function connect() {
|
23
|
$this->dbh = new PDO("mysql:host=".$this->dbhost.";dbname=".$this->dbname.";charset=utf8", $this->user, $this->pass);
|
24
|
if (!$this->dbh) {
|
25
|
throw new DB_Exception($this->logs);
|
26
|
}
|
27
|
}
|
28
|
|
29
|
public function executeQuery($query) {
|
30
|
if (!$this->dbh) {
|
31
|
$this->connect();
|
32
|
}
|
33
|
$ret = $this->dbh->query($query);
|
34
|
if (!$ret) {
|
35
|
throw new DB_Exception($this->logs);
|
36
|
} else {
|
37
|
$stmt = new DB_PDOStatement($this->dbh, $query, $this->logs);
|
38
|
$stmt->result = $ret;
|
39
|
$stmt->number = $ret->rowCount();
|
40
|
return $stmt;
|
41
|
}
|
42
|
}
|
43
|
|
44
|
}
|
45
|
|
46
|
class DB_PDOStatement {
|
47
|
|
48
|
public $result;
|
49
|
public $query;
|
50
|
public $number;
|
51
|
protected $dbh;
|
52
|
|
53
|
private $logs;
|
54
|
|
55
|
public function __construct($dbh, $query, $logs) {
|
56
|
$this->query = $query;
|
57
|
$this->dbh = $dbh;
|
58
|
$this->logs = $logs;
|
59
|
if (!$dbh) {
|
60
|
throw new DB_Exception($this->logs, "Spojení s databází se nezdařilo!");
|
61
|
}
|
62
|
}
|
63
|
|
64
|
public function fetchAssoc() {
|
65
|
if (!$this->result) {
|
66
|
throw new DB_Exception($this->logs, "Dotaz nebyl vykonán!");
|
67
|
}
|
68
|
return $this->result->fetch(PDO::FETCH_ASSOC);
|
69
|
}
|
70
|
|
71
|
}
|
72
|
|
73
|
?>
|