Projekt

Obecné

Profil

Stáhnout (1.27 KB) Statistiky
| Větev: | Revize:
1
<?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
    function select($sql, $cond=null){
27
        $result = false;
28
        try {
29
            $this->stmt = $this->pdo->prepare($sql);
30
            $this->stmt->execute($cond);
31
            $result = $this->stmt->fetchAll();
32
            return $result;
33
        } catch (Exception $ex) {
34
            $this->error = $ex->getMessage();
35
            return false;
36
        }
37
    }
38
}
39

    
40
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
41
define('DB_HOST', 'localhost');
42
define('DB_NAME', 'dalimil1');
43
define('DB_CHARSET', 'utf8');
44
define('DB_USER', 'postgres');
45
define('DB_PASSWORD', 'a');
    (1-1/1)