Advertisement
Guest User

Untitled

a guest
Aug 5th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <?php
  2.  
  3.     $db = Database::connect("localhost","skata","kimonas1_test","test");
  4.  
  5.  
  6.     class Database{
  7.    
  8.         private static $username;
  9.         private static $password;
  10.         private static $database;
  11.         private static $url;
  12.         private static $instance;
  13.         private static $connection;
  14.        
  15.     private function __construct(){
  16.        
  17.         if($con = self::SQLConnect(self::$url,self::$username,self::$password) != false)
  18.         {
  19.             self::$connection = $con;
  20.             echo "Connection established";
  21.            
  22.             if($db = self::selectDB(self::$database,$con) != false)
  23.             {
  24.                 echo "Connection to database established.";
  25.             }
  26.             else
  27.             {
  28.                 return false;
  29.             }
  30.         }
  31.         else
  32.         {
  33.             return false;
  34.         }
  35.    
  36.     }
  37.    
  38.     public static function connect($url,$password,$username,$database){
  39.  
  40.         if (!isset(self::$instance)) {
  41.             $class = __CLASS__;
  42.             self::$username = $username;
  43.             self::$password = $password;
  44.             self::$url = $url;
  45.             self::$database = $database;
  46.             self::$instance = new $class;
  47.         }
  48.  
  49.         return self::$instance;
  50.  
  51.     }  
  52.    
  53.     private static function SQLConnect($url,$username,$password){
  54.  
  55.         $con = mysql_connect($url,$username,$password);
  56.         if(!$con)
  57.         {
  58.             return false;
  59.         }
  60.         else
  61.         {
  62.             return $con;
  63.         }
  64.     }
  65.    
  66.     function selectDB($database,$con){
  67.            
  68.         if(isset(self::$connection))
  69.         {
  70.             $sel = mysql_select_db($database,$con);
  71.             if(!$sel)
  72.             {
  73.                 return false;
  74.             }
  75.             else
  76.             {
  77.                 return $sel;
  78.             }
  79.         }
  80.         else
  81.         {
  82.             return false;
  83.         }
  84.     }
  85.  
  86.  
  87.    
  88.    
  89.    
  90.    
  91.    
  92.    
  93.    
  94.     }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement