-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_model.php
41 lines (36 loc) · 1.21 KB
/
db_model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
class DatabaseAdaptor
{
private $DB;
public function __construct()
{
$db = 'mysql:dbname=RNGTube; charset=utf8; host=127.0.0.1';
$auth = json_decode(file_get_contents("dbAuth.json"), true);
$user = $auth['user'];
$password = $auth['password'];
try {
$this->DB = new PDO($db, $user, $password);
$this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo ('Error establishing Connection');
exit();
}
}
public function getVideoCategories()
{
$stmt = $this->DB->prepare("SELECT DISTINCT category FROM videos");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function getVideos($filter, $limit)
{
if ($filter === 'Mix') {
$stmt = $this->DB->prepare("SELECT * FROM videos ORDER BY RAND() LIMIT ".$limit.";");
} else {
$stmt = $this->DB->prepare("SELECT * FROM videos WHERE category = '" . $filter . "' ORDER BY RAND() LIMIT ".$limit.";");
}
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
?>