Jums jāizstrādā kāda lielāk projekta prototips. Izstrādājot prototipu, paturiet prātā, ka projektam attīstoties, šo prototipu varētu vajadzēt pilnveidot.
<?php
class Database
{
private $hostname;
private $username;
private $password;
private $dbName;
public function __construct()
{
$config = new Configuration();
$this->hostname = $config->getDbHostname();
$this->username = $config->getDbUsername();
$this->password = $config->getDbPassword();
$this->dbName = $config->getDbName();
}
public function getServerConnection()
{
try
{
$db_connect = new mysqli($this->hostname, $this->username, $this->password);
if ($db_connect->connect_error)
{
die("Database connection failed: ".$db_connect->connect_error);
}
return $db_connect;
}
catch (Exception $e)
{
echo "Caught exception: ", $e->getMessage(), "\r\n";
}
return false;
}
public function getConn()
{
try
{
$db_connect = new mysqli($this->hostname, $this->username, $this->password, $this->dbName);
if ($db_connect->connect_error)
{
return false;
//die("Database connection failed: ".$db_connect->connect_error);
}
mysqli_select_db($db_connect, $this->dbName);
return $db_connect;
}
catch (Exception $e)
{
echo "Caught exception: ", $e->getMessage(), "\r\n";
throw new Exception("Database not exists.");
}
return false;
}
public function query($conn, $sql)
{
mysqli_query($conn, $sql);
}
public function execute($sql)
{
mysqli_query($this->getConn(), $sql);
}
public function executeSql($sql)
{
try
{
mysqli_query($this->getConn(), $sql);
}
catch (Exception $e)
{
echo "Caught exception: ", $e->getMessage(), "\r\n";
}
}
public function sanitize($var)
{
$var = Helper::sanitizeInput($var);
return mysqli_real_escape_string($this->getConn(), $var);
}
}