Skip to content
Snippets Groups Projects
Commit b9cb7e7f authored by Raghav's avatar Raghav
Browse files

Connect DB with class and Tested if conn exist return exist or create conn

parent d5971b39
No related branches found
No related tags found
No related merge requests found
<?php
class Database
{
public static $conn = null;
public static function getConnection()
{
if (Database::$conn == null) {
$servername = "mysql.selfmade.ninja";
$username = "raghav";
$password = "R1a2g1h3av";
$dbname = "raghav_photogram";
// Create connection in $conn variable
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); //TODO: we can manage this error with exception handling in future classes...
} else {
// Returning the existing data of $conn
print("Connected newly");
Database::$conn = $conn;
return Database::$conn;
}
} else {
// If database already connected it returns the existing value of $conn
print("Returned existed connection");
return Database::$conn;
}
}
}
......@@ -2,6 +2,7 @@
// To include class files automatically
include_once '_includes/Mic.class.php';
include_once '_includes/Database.class.php';
function load_template($name)
{
......@@ -22,17 +23,8 @@ function validate_credentials($username, $password)
// Connecting to database
function signup($user, $pass, $email, $phone)
{
$servername = "mysql.selfmade.ninja";
$username = "raghav";
$password = "R1a2g1h3av";
$dbname = "raghav_photogram";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Store $conn values from class Database
$conn = Database::$conn;
$sql = "INSERT INTO `auth` (`username`, `password`, `email`, `phone`, `block`, `active`)
VALUES ('$user', '$pass', '$email', '$phone', '0', '1');";
......
......@@ -45,6 +45,10 @@
$mic1 -> setColorProxy("Red");
print("From private color: ". $mic1-> getColorProxy());
// Check whether connection is established or returns existing connection
Database::getConnection();
Database::getConnection();
Database::getConnection();
?>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment