Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1. <?php
  2.  
  3. class MySQL {
  4.     private $hostname;
  5.     private $username;
  6.     private $password;
  7.     private $database;
  8.     private $link;
  9.     private $result;
  10.     private $config = false;
  11.    
  12.     public function __construct() {
  13.        
  14.     }
  15.    
  16.     public function close() {
  17.         mysql_close($this->link);
  18.     }
  19.    
  20.     public function connect($hostname = null, $username = null, $password = null) {
  21.         if (func_num_args() == 0) {
  22.             if ($this->config) {
  23.                 $hostname = $this->hostname;
  24.                 $username = $this->username;
  25.                 $password = $this->password;
  26.             } else {
  27.                 die("MySQL::connect() failed -- the config is missing.");
  28.             }
  29.         }
  30.        
  31.         $this->link = mysql_connect($hostname, $username, $password);
  32.        
  33.         if (!$this->link) {
  34.             die("MySQL::connect() failed -- the connection couldn't be established: ".mysql_error());
  35.         }
  36.     }
  37.    
  38.     public function fetchRow() {
  39.         return mysql_fetch_row($this->result);
  40.     }
  41.    
  42.     public function loadConfig($config = "config.php") {
  43.         require_once $config;
  44.        
  45.         if (!defined("HOSTNAME") || !defined("USERNAME") || !defined("PASSWORD") || !defined("DATABASE")) {
  46.             die("MySQL::loadConfig() failed -- the required configuration constants couldn't be found.");
  47.         }
  48.        
  49.         $this->hostname = HOSTNAME;
  50.         $this->username = USERNAME;
  51.         $this->password = PASSWORD;
  52.         $this->database = DATABASE;
  53.         $this->config = true;
  54.     }
  55.    
  56.     public function query($query) {
  57.         $this->result = mysql_query($query);
  58.        
  59.         if (!$this->result) {
  60.             die("MySQL::query -- invalid query: " . mysql_error());
  61.         }
  62.     }
  63.    
  64.     public function selectDatabase($database = null) {
  65.         if (func_num_args() == 0) {
  66.             if ($this->config) {
  67.                 $database = $this->database;
  68.             } else {
  69.                 die("MySQL::selectDatabase() failed -- the config is missing.");
  70.             }
  71.         }
  72.        
  73.         $selected = mysql_select_db($database, $this->link);
  74.  
  75.         if (!$selected) {
  76.             die ("MySQL::selectDatabase() failed -- the database couldn't be selected: ".mysql_error());
  77.         }
  78.     }
  79. }
  80.  
  81. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement