Projekt

Obecné

Profil

Stáhnout (2.46 KB) Statistiky
| Větev: | Revize:
1 3b343aea Tomáš Pašek
<?php
2
class DB {
3
    // (A) CONNECT TO DATABASE
4
    public $error = "";
5
    private $pdo = null;
6
    private $stmt = null;
7
    function __construct () {
8
        try {
9
            $this->pdo = new PDO(
10
                "pgsql:host=".DB_HOST.";dbname=".DB_NAME,
11
                DB_USER, DB_PASSWORD, [
12
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
13
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
14
                ]
15
            );
16
        } catch (Exception $ex) { die($ex->getMessage()); }
17
    }
18
19
    // (B) CLOSE CONNECTION
20
    function __destruct(){
21
        if ($this->stmt!==null) { $this->stmt = null; }
22
        if ($this->pdo!==null) { $this->pdo = null; }
23
    }
24
25
    // (C) RUN A SELECT QUERY
26 2eab68b9 Tomáš Pašek
    function select($sql, $params){
27 3b343aea Tomáš Pašek
        $result = false;
28
        try {
29
            $this->stmt = $this->pdo->prepare($sql);
30 2eab68b9 Tomáš Pašek
            if (array_key_exists("lemma", $params)) {
31
                $this->stmt->bindParam(':lemma',$params['lemma'], PDO::PARAM_STR);
32
            }
33
            if (array_key_exists("word", $params)) {
34
                $this->stmt->bindParam(':word',$params['word'], PDO::PARAM_STR);
35
            }
36
            if (array_key_exists("position1", $params)) {
37
                $this->stmt->bindParam(':position1',$params['position1'], PDO::PARAM_INT);
38
            }
39
            if (array_key_exists("position2", $params)) {
40
                $this->stmt->bindParam(':position2',$params['position2'], PDO::PARAM_INT);
41
            }
42
            if (array_key_exists("positiondetail", $params)) {
43
                $this->stmt->bindParam(':positiondetail',$params['positiondetail'], PDO::PARAM_INT);
44
            }
45
            if (array_key_exists("tag", $params)) {
46
                $this->stmt->bindParam(':tag',$params['tag'], PDO::PARAM_STR);
47
            }
48
            if (array_key_exists("manuscript", $params)) {
49
                for ($x = 0; $x < count($params["manuscript"]); $x += 1) {
50
                    $this->stmt->bindParam(':manuscript'.$x,$params["manuscript"][$x], PDO::PARAM_INT);
51
                }
52
            }
53
            $this->stmt->execute();
54 3b343aea Tomáš Pašek
            $result = $this->stmt->fetchAll();
55
            return $result;
56
        } catch (Exception $ex) {
57
            $this->error = $ex->getMessage();
58
            return false;
59
        }
60
    }
61
}
62
63
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
64
define('DB_HOST', 'localhost');
65 2eab68b9 Tomáš Pašek
define('DB_NAME', 'dalimil2');
66 3b343aea Tomáš Pašek
define('DB_CHARSET', 'utf8');
67
define('DB_USER', 'postgres');
68
define('DB_PASSWORD', 'a');