db.php 1.58 KB
Newer Older
stc's avatar
stc committed
1
2
3
4
5
6
<?php

class DB {

	private $conn;

Rémy Huet's avatar
Rémy Huet committed
7
	public function __construct () {
stc's avatar
stc committed
8
9
10
11
12
13
14
15
		include 'connexion.php';
		try {
			$this->conn = new PDO('pgsql:host=localhost;port=5432;dbname=apisub', $db_user, $db_pass);
		} catch (PDOException $e) {
			die('Connection failed: ' . $e->getMessage());
		}
	}

16
	public function subList($utclogin) {
17
		$sql = 'SELECT *
Stephane Crozat's avatar
Stephane Crozat committed
18
						FROM vsubscription
19
						WHERE	utclogin=:utclogin';
stc's avatar
stc committed
20
		$st = $this->conn->prepare($sql);
21
22
		$st->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
		$st->execute();
stc's avatar
stc committed
23
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
24
		return $res;
stc's avatar
stc committed
25
26
	}

27
	public function apiList($semester, $year) {
28
		$sql = 'SELECT *
29
						FROM vapi
30
						WHERE semester=:semester AND year=:year';
stc's avatar
stc committed
31
		$st = $this->conn->prepare($sql);
32
33
34
		$st->bindValue(':semester',$semester,PDO::PARAM_STR);
		$st->bindValue(':year',$year,PDO::PARAM_INT);
		$st->execute();
stc's avatar
stc committed
35
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
36
		return $res;
stc's avatar
stc committed
37
38
	}

39
	public function subToApi($utclogin, $api) {
40
41
42
		$today = date('Ymd');
		$sql = 'INSERT INTO subscribe(utclogin, api, subdate) VALUES (:utclogin, :api, :today)';
		$st = $this->conn->prepare($sql);
43
44
45
46
		$st->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
		$st->bindValue(':api',$api,PDO::PARAM_INT);
		$st->bindValue(':today',$today,PDO::PARAM_STR);
		$res = $st->execute();
47
		return $res;
stc's avatar
stc committed
48
49
	}

50
	public function unsubToApi($utclogin, $api) {
51
52
		$sql = 'DELETE FROM subscribe WHERE utclogin=:utclogin AND api=:api';
		$st = $this->conn->prepare($sql);
53
54
55
		$st->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
		$st->bindValue(':api',$api,PDO::PARAM_INT);
		$res = $st->execute();
56
57
		return $res;
	}
stc's avatar
stc committed
58
}