-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.php
63 lines (52 loc) · 2.16 KB
/
database.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
59
60
61
62
63
<?php
/****************************************************************************
* The Database class handles access to a database. Usernames and passwords *
* are NOT handled by this class. Instead the constructor 'requires' a file *
* that simply declares them like this: *
* *
* $username = "luggage"; *
* $password = "12345"; *
* *
* Such a file's path is hard-coded. We don't want to programmatically be *
* able to find and read that file. Also, make sure to use 'require_once'. *
****************************************************************************/
abstract class Database
{
// The database's name shouldn't change unless you derive the class.
protected $dbname = "dimentions";
// The connection is private so that we can limit direct access.
private $connection;
// The last query statement used.
private $statement;
public function __construct()
{
require_once("../../password.php");
$string = "mysql:host=localhost;dbname={$this->dbname}";
$options = [PDO::ATTR_PERSISTENT => true];
$this->connection = new PDO($string, $username, $password, $options);
}
// This method sets up a query statement. Only classes that extend this
// class can access it.
final protected function query($query)
{
$this->statement = $this->connection->prepare($query);
}
// Binds the given parameters to the query statement. Any number of
// parameters can be given, and they are passed as a reference rather
// than a value. Only classes that extend this class may access it.
final protected function bind(&...$parameters)
{
$i = 1;
foreach ($parameters as &$parameter) {
$this->statement->bindParam($i, $parameter);
$i++;
}
}
// This method performs the query and returns all of the results. Only
// classes extending this class can use it.
final protected function execute()
{
$this->statement->execute();
return $this->statement->fetchAll();
}
}