Add php files to communicate with the database

All previous routes of the nodejs server are now working with php.
This commit is contained in:
dario-cfpt
2020-03-09 20:09:33 +01:00
parent abfd944bbf
commit a561ac89f3
4 changed files with 270 additions and 0 deletions

43
server/database.php Normal file
View File

@ -0,0 +1,43 @@
<?php
// Singleton to connect db.
class database
{
// Hold the class instance.
private static $instance = null;
private $conn;
private $host = 'localhost';
private $user = 'root';
private $pass = '';
private $name = 'fe_charts';
private $port = '';
// The db connection is established in the private constructor.
private function __construct()
{
try {
$this->conn = new PDO(
"mysql:host={$this->host};dbname={$this->name};port={$this->port}",
$this->user,
$this->pass
);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
}
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new database();
}
return self::$instance;
}
public function getConnection()
{
return $this-> conn;
}
}