Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1. <?php
  2. class Database
  3. {
  4.     /* Singleton ***********************************/
  5.     private static $instance;
  6.    
  7.     public static function init($name, $server, $user, $password)
  8.     {
  9.         self::$instance = new Database($name, $server, $user, $password);
  10.     }
  11.    
  12.     public static function get()
  13.     {
  14.         if(!self::$instance->is_open()) self::$instance->open();
  15.         return self::$instance;
  16.     }
  17.    
  18.     /* Instance ***********************************/
  19.     private $name, $server, $user, $password = '';
  20.     private $connection = NULL;
  21.    
  22.     public function Database($name, $server, $user, $password)
  23.     {
  24.         $this->name = $name;
  25.         $this->server = $server;
  26.         $this->user = $user;
  27.         $this->password = $password;
  28.     }
  29.    
  30.     public function is_open()
  31.     {
  32.         return ($this->connection != NULL);
  33.     }
  34.    
  35.     public function open()
  36.     {
  37.         $this->connection = mysql_connect($this->server, $this->user, $this->password) or $this->dump_error('none');
  38.         mysql_select_db($this->name, $this->connection) or die('Database "' . $this->name . '" does not exist! Contact system administrator.');
  39.     }
  40.    
  41.     public function close()
  42.     {
  43.         if($this->connection != NULL)
  44.         {
  45.             mysql_close($this->connection);
  46.             $this->connection = NULL;
  47.         }
  48.     }
  49.    
  50.     public function row($query)
  51.     {
  52.         $result = mysql_query($query, $this->connection) or $this->dump_error($query);
  53.         if(mysql_num_rows($result) > 0)
  54.         {
  55.             return mysql_fetch_assoc($result);
  56.         }
  57.         else
  58.         {
  59.             return NULL;
  60.         }
  61.     }
  62.    
  63.     public function rows($query)
  64.     {
  65.         $result = mysql_query($query, $this->connection) or $this->dump_error($query);
  66.         $rows = array();
  67.         if(mysql_num_rows($result) > 0)
  68.         {
  69.             while($row = mysql_fetch_assoc($result))
  70.             {
  71.                 $rows[] = $row;
  72.             }
  73.         }
  74.         return $rows;
  75.     }
  76.    
  77.     public function execute($query)
  78.     {
  79.         return mysql_query($query, $this->connection) or $this->dump_error($query);
  80.     }
  81.    
  82.     public function last_inserted_id()
  83.     {
  84.         return mysql_insert_id($this->connection);
  85.     }
  86.    
  87.     private function dump_error($query)
  88.     {
  89.         ?>
  90.  
  91.         <div id="error">
  92.             <h3>Database error!</h3>
  93.             <p><strong>Query:</strong> <em><?=$query?></em></p>
  94.             <p><strong>Message:</strong> <em><?=mysql_error()?></em></p>
  95.         </div>
  96.        
  97.         <?php
  98.     }
  99. }
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement