diff --git a/_templates/_signup.php b/_templates/_signup.php index 2a6c79840d60dfc208d73881f9ffcabfd45b52c2..94400eabc9770f16c4192dc2ab61e55b6b7cf559 100644 --- a/_templates/_signup.php +++ b/_templates/_signup.php @@ -6,7 +6,7 @@ if (isset($_POST['username']) and isset($_POST['password']) and !empty($_POST['p $password = $_POST['password']; $email = $_POST['email_address']; $phone = $_POST['phone']; - $error = signup($username, $password, $email, $phone); + $error = User::signup($username, $password, $email, $phone); $signup = true; } ?> diff --git a/libs/includes/Database.class.php b/libs/includes/Database.class.php new file mode 100644 index 0000000000000000000000000000000000000000..90e4c78dc3a9c3705b40a0ecc957e5aa03bf347c --- /dev/null +++ b/libs/includes/Database.class.php @@ -0,0 +1,29 @@ +<?php + +class Database +{ + public static $conn = null; + public static function getConnection() + { + if (Database::$conn == null) { + $servername = "mysql.selfmade.ninja"; + $username = "sibidharan"; + $password = "xyjxo8-xefjat-gYnsif"; + $dbname = "sibidharan_newdb"; + + // Create connection + $connection = new mysqli($servername, $username, $password, $dbname); + // Check connection + if ($connection->connect_error) { + die("Connection failed: " . $connection->connect_error); //TODO: Replace this with exception handling + } else { + printf("New connection establishing..."); + Database::$conn = $connection; //replacing null with actual connection + return Database::$conn; + } + } else { + printf("Returning existing establishing..."); + return Database::$conn; + } + } +} diff --git a/libs/includes/Mic.class.php b/libs/includes/Mic.class.php index 2cd09354239bf07a20970c8ce23d97d7916bdd35..2c62fe3a74bdf6372c1decca67b3a6d6b1fd0ac5 100644 --- a/libs/includes/Mic.class.php +++ b/libs/includes/Mic.class.php @@ -16,11 +16,18 @@ class Mic public $model; private $light; public $price; + public static $test; + + public static function testFunction() + { + printf("This is a static function, this can be run without object initialization. "); + } public function __construct($brand) { printf("Constructing object..."); $this->brand = ucwords($brand); + Mic::testFunction(); } public function setLight($light) @@ -52,4 +59,22 @@ class Mic { return $this->getModel(); } + + public function __destruct() + { + printf("Destruct object: brand: $this->brand..."); + } +} + +class DupMic +{ + public static function testFunction() + { + return "hello"; + } +} + +function testFunction() +{ + printf("This is a static function, this can be run without object initialization."); } diff --git a/libs/includes/User.class.php b/libs/includes/User.class.php index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..eee51f5b614d1c00ae4b5c653048e0a36dafec76 100644 --- a/libs/includes/User.class.php +++ b/libs/includes/User.class.php @@ -0,0 +1,21 @@ +<?php + +class User +{ + public static function signup($user, $pass, $email, $phone) + { + $conn = Database::getConnection(); + $sql = "INSERT INTO `auth` (`username`, `password`, `email`, `phone`, `active`) + VALUES ('$user', '$pass', '$email', '$phone', '1');"; + $error = false; + if ($conn->query($sql) === true) { + $error = false; + } else { + // echo "Error: " . $sql . "<br>" . $conn->error; + $error = $conn->error; + } + + // $conn->close(); + return $error; + } +} diff --git a/libs/load.php b/libs/load.php index 9a434999eada86a41cff3313b3032f315334a3f3..e9d06e1c9a8d3e46c8637f44f3bc5cabaed33e26 100644 --- a/libs/load.php +++ b/libs/load.php @@ -1,6 +1,7 @@ <?php include_once 'includes/Mic.class.php'; +include_once 'includes/Database.class.php'; function load_template($name) { @@ -15,31 +16,3 @@ function validate_credentials($username, $password) return false; } } - -function signup($user, $pass, $email, $phone) -{ - $servername = "mysql.selfmade.ninja"; - $username = "sibidharan"; - $password = "xyjxo8-xefjat-gYnsif"; - $dbname = "sibidharan_newdb"; - - // Create connection - $conn = new mysqli($servername, $username, $password, $dbname); - // Check connection - if ($conn->connect_error) { - die("Connection failed: " . $conn->connect_error); - } - - $sql = "INSERT INTO `auth` (`username`, `password`, `email`, `phone`, `active`) - VALUES ('$user', '$pass', '$email', '$phone', '1');"; - $error = false; - if ($conn->query($sql) === true) { - $error = false; - } else { - // echo "Error: " . $sql . "<br>" . $conn->error; - $error = $conn->error; - } - - $conn->close(); - return $error; -} diff --git a/test.php b/test.php index f3a020109be64e274d039af0c3f1d432ab5f82ab..7259da6cf1b796e19cefe0e11879e364dbfe8f1c 100644 --- a/test.php +++ b/test.php @@ -25,6 +25,8 @@ include 'libs/load.php'; $mic1 = new Mic("Roda"); //constructing the object $mic2 = new Mic("HyperX"); //constructing the object +Mic::testFunction(); //no construction, no destruction. +testFunction(); $mic1->setLight("White"); $mic2->setLight("Green"); @@ -36,5 +38,16 @@ print("\n".$mic1->getBrand()); print("\n".$mic2->getBrand()); print("Value of 10+12 is ".$mic1->add(10, 12)); +print("This is mono font inside pre tag \n"); + +$conn = Database::getConnection(); +$conn = Database::getConnection(); +$conn = Database::getConnection(); +$conn = Database::getConnection(); +$conn = Database::getConnection(); +$conn = Database::getConnection(); +$conn = Database::getConnection(); + ?> -</pre> \ No newline at end of file +</pre> +This is regular font. \ No newline at end of file