Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 574d6347

Přidáno uživatelem horkym před asi 7 roky(ů)

#6345: Realizace databaze

Pridany tridy zajistujici komunikaci s databazi.

Zobrazit rozdíly:

cron/dao/dao.php
1
<?php
2

  
3
require_once "db/db-web.php";
4

  
5
class DAO {
6
  
7
    protected $dbh;
8
    
9
    public function setDB($dbh) {
10
        $this->dbh = $dbh;
11
    }
12
    
13
}
14

  
15
?>
cron/db/db-exception.php
1
<?php
2

  
3
class DB_Exception extends Exception {
4
    
5
    public function __construct($message = false, $code = false) {
6
        if (!$message) {
7
            $this->message = mysql_error();
8
        } 
9
        if (!$code) {
10
            $this->code = mysql_errno();
11
        }
12
    }
13
    
14
}
15

  
16
?>
cron/db/db-pdo.php
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
    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
?>
cron/db/db-web.php
1
<?php
2

  
3
require_once "db-pdo.php";
4

  
5
class DB_WEB extends DB_PDO {
6
  
7
    protected $user = "root";
8
    protected $pass = "";
9
    protected $dbhost = "localhost";
10
    protected $dbname = "prujezd_vozidel";
11
  
12
    public function __construct() {
13
    }
14
     
15
}
16

  
17
?>

Také k dispozici: Unified diff