<?php
class Database
{
    // specify your own database credentials
    private $host = "localhost";            //Server
    private $db_name = "ProserveAPI";       //Database Name
    private $username = "root";             //UserName of Phpmyadmin
    private $password = "";                 //Password associated with username
    public $conn;

    // shared connection re-used across endpoint includes within a single request,
    // and (with PDO::ATTR_PERSISTENT) across requests within an Apache child process.
    private static $sharedConn = null;

    // get the database connection
    public function getConnection()
    {
        if (self::$sharedConn === null)
        {
            try
            {
                self::$sharedConn = new PDO(
                    "mysql:host=" . $this->host . ";dbname=" . $this->db_name,
                    $this->username,
                    $this->password,
                    [PDO::ATTR_PERSISTENT => true]
                );
                self::$sharedConn->exec("set names utf8");
            }
            catch(PDOException $exception)
            {
                echo "Connection error: " . $exception->getMessage();
                return null;
            }
        }
        $this->conn = self::$sharedConn;
        return self::$sharedConn;
    }
}
?>
