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