Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 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.         if (func_num_args() > 1) {
  58.             for ($i = 1; $i < func_num_args(); $i++) {
  59.                 $args[] = mysql_real_escape_string(func_get_arg($i));
  60.             }
  61.            
  62.             $query = vsprintf($query, $args);
  63.         }
  64.        
  65.         $this->result = mysql_query($query);
  66.        
  67.         if (!$this->result) {
  68.             die("MySQL::query -- invalid query: " . mysql_error());
  69.         }
  70.     }
  71.    
  72.     public function rows() {
  73.         return mysql_num_rows($this->result);
  74.     }
  75.    
  76.     public function selectDatabase($database = null) {
  77.         if (func_num_args() == 0) {
  78.             if ($this->config) {
  79.                 $database = $this->database;
  80.             } else {
  81.                 die("MySQL::selectDatabase() failed -- the config is missing.");
  82.             }
  83.         }
  84.        
  85.         $selected = mysql_select_db($database, $this->link);
  86.  
  87.         if (!$selected) {
  88.             die ("MySQL::selectDatabase() failed -- the database couldn't be selected: ".mysql_error());
  89.         }
  90.     }
  91. }
  92.  
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement