db.php 1.12 KB
Newer Older
stc's avatar
stc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

class DB {

	private $conn;

	function __construct () {
		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
	function subList(string $utclogin) : array {
17
		$sql = "SELECT *
Stephane Crozat's avatar
Stephane Crozat committed
18
						FROM vsubscription
19
						WHERE	utclogin='".$utclogin."'";
stc's avatar
stc committed
20
21
22
		$st = $this->conn->prepare($sql);
		$st->execute();
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
23
24

		return $res;
stc's avatar
stc committed
25
26
	}

27
	function apiList(string $semester, int $year) : array {
28
29
		$sql = "SELECT *
						FROM vApi
Stephane Crozat's avatar
Stephane Crozat committed
30
						WHERE semester='".$semester."' AND year=".$year;
stc's avatar
stc committed
31
32
33
		$st = $this->conn->prepare($sql);
		$st->execute();
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
34
35
		
		return $res;
stc's avatar
stc committed
36
37
38
39
40
41
42
43
44
	}

	function subToApi($utclogin, $api) {
		$today = date("Ymd");
		$sql = "INSERT INTO subscribe VALUES ('".$utclogin."',".$api.",TO_DATE('".$today."','YYYYMMDD'))";
		$res=$this->conn->exec($sql);
	}

	function unsubToApi($utclogin, $api) {
45
		$sql = "DELETE FROM subscribe WHERE utclogin='".$utclogin."' AND api=".$api;
stc's avatar
stc committed
46
47
48
49
50
		$res=$this->conn->exec($sql);
	}


}