1 |
574d6347
|
horkym
|
<?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 |
7cee2250
|
horkym
|
|
13 |
|
|
protected $logs;
|
14 |
574d6347
|
horkym
|
|
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 |
7cee2250
|
horkym
|
throw new DB_Exception($this->logs);
|
26 |
574d6347
|
horkym
|
}
|
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 |
7cee2250
|
horkym
|
throw new DB_Exception($this->logs);
|
36 |
574d6347
|
horkym
|
} else {
|
37 |
7cee2250
|
horkym
|
$stmt = new DB_PDOStatement($this->dbh, $query, $this->logs);
|
38 |
574d6347
|
horkym
|
$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 |
7cee2250
|
horkym
|
|
53 |
|
|
private $logs;
|
54 |
574d6347
|
horkym
|
|
55 |
7cee2250
|
horkym
|
public function __construct($dbh, $query, $logs) {
|
56 |
574d6347
|
horkym
|
$this->query = $query;
|
57 |
|
|
$this->dbh = $dbh;
|
58 |
7cee2250
|
horkym
|
$this->logs = $logs;
|
59 |
574d6347
|
horkym
|
if (!$dbh) {
|
60 |
7cee2250
|
horkym
|
throw new DB_Exception($this->logs, "Spojení s databází se nezdařilo!");
|
61 |
574d6347
|
horkym
|
}
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
public function fetchAssoc() {
|
65 |
|
|
if (!$this->result) {
|
66 |
7cee2250
|
horkym
|
throw new DB_Exception($this->logs, "Dotaz nebyl vykonán!");
|
67 |
574d6347
|
horkym
|
}
|
68 |
|
|
return $this->result->fetch(PDO::FETCH_ASSOC);
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
?>
|