-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.class.php
58 lines (50 loc) · 994 Bytes
/
database.class.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
58
<?php
/*
* DB - A database class
*
* @author Jonatan Saari
*
*/
use \PDO;
class DB
{
public $pdo;
public function __construct(string $username, string $password, string $database = "")
{
if($database == "")
{
$this->pdo = new PDO("mysql:host=127.0.0.1;port=3306;charset=utf8;",
$username,
$password
);
}
else
{
$this->pdo = new PDO("mysql:host=127.0.0.1;port=3306;charset=utf8;dbname=$database;",
$username,
$password
);
}
}
// Secured query
public function query($sql, $bind = [])
{
$statement = $this->pdo->prepare($sql);
foreach ($bind as $name => $value)
{
$statement->bindValue(':' . $name, $value);
}
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
// Secured execute
public function exec(string $sql, $bind = [])
{
$statement = $this->pdo->prepare($sql);
foreach ($bind as $name => $value)
{
$statement->bindValue(':' . $name, $value);
}
$statement->execute();
}
}