-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
32 lines (26 loc) · 918 Bytes
/
Database.php
File metadata and controls
32 lines (26 loc) · 918 Bytes
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
<?php
class Database {
// Database configuration
private $host = 'localhost';
private $dbname = 'db';
private $username = 'root';
private $password = '';
// Table used in the database
public static $table = 'users';
// Connect to database
public function connect() {
$pdo = null;
// Setting Data Source Name (dsn)
$dsn = "mysql:host=$this->host; dbname=$this->dbname; charset=UTF8";
try {
// Create a PHP Data Object (pdo)
$pdo = new PDO($dsn, $this->username, $this->password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch(PDOException $e) {
http_response_code(500); // Internal Server Error
echo "Connection Error: {$e->getMessage()}";
}
return $pdo;
}
} // end: class Database