forked from lakridserne/teaminator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbConnect.php
57 lines (53 loc) · 1.3 KB
/
dbConnect.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* Coding Pirates Teaminator
* Used to generate teams at Coding Pirates Game Jam 2015-2016
*/
class DB{
function query($sql,$values=false) {
$db = $this->connect();
$query = $db->prepare($sql);
if($values != false) {
foreach($values as list($name,$value)) {
$query->bindValue($name, $value);
}
}
$query->execute();
if($query->rowCount() > 0) {
$results = $query->fetchAll();
return $results;
}
}
// Count
function count($sql,$values=false) {
$db = $this->connect();
$query = $db->prepare($sql);
if($values != false) {
foreach($values as list($name,$value)) {
$query->bindValue($name, $value);
}
}
$query->execute();
return $query->rowCount();
}
// Connect to db
private function connect() {
// first, get config file
if(file_exists("config.php")) {
include_once("config.php");
} else {
die("No config file");
}
$config = new config;
$dsn = "mysql:dbname=" . $config->get_db() . ";host=" . $config->get_host() . ";charset=utf8mb4";
$user = $config->get_user();
$password = $config->get_pass();
try {
$db = new PDO($dsn,$user,$password);
} catch(PDOException $e) {
echo "Databasefejl " . $e->getMessage();
}
return $db;
}
}
?>