Guest User

Untitled

a guest
Dec 2nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.40 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 fetchArray($return = MYSQL_ASSOC) {
  39.         $array = null;
  40.        
  41.         while ($row = mysql_fetch_array($this->result, $return)) {
  42.             $array[] = $row;
  43.         }
  44.        
  45.         return $array;
  46.     }
  47.    
  48.     public function fetchRow($return = MYSQL_ASSOC) {
  49.         $array = $this->fetchArray($return);
  50.         return $array[0];
  51.     }
  52.    
  53.     public function loadConfig($config = "config.php") {
  54.         require_once $config;
  55.        
  56.         if (!defined("HOSTNAME") || !defined("USERNAME") || !defined("PASSWORD") || !defined("DATABASE")) {
  57.             die("MySQL::loadConfig() failed -- the required configuration constants couldn't be found.");
  58.         }
  59.        
  60.         $this->hostname = HOSTNAME;
  61.         $this->username = USERNAME;
  62.         $this->password = PASSWORD;
  63.         $this->database = DATABASE;
  64.         $this->config = true;
  65.     }
  66.    
  67.     public function query($query) {
  68.         if (func_num_args() > 1) {
  69.             for ($i = 1; $i < func_num_args(); $i++) {
  70.                 $args[] = mysql_real_escape_string(func_get_arg($i));
  71.             }
  72.            
  73.             $query = vsprintf($query, $args);
  74.         }
  75.        
  76.         $this->result = mysql_query($query);
  77.        
  78.         if (!$this->result) {
  79.             die("MySQL::query -- invalid query: " . mysql_error());
  80.         }
  81.     }
  82.    
  83.     public function rows() {
  84.         return mysql_num_rows($this->result);
  85.     }
  86.    
  87.     public function selectDatabase($database = null) {
  88.         if (func_num_args() == 0) {
  89.             if ($this->config) {
  90.                 $database = $this->database;
  91.             } else {
  92.                 die("MySQL::selectDatabase() failed -- the config is missing.");
  93.             }
  94.         }
  95.        
  96.         $selected = mysql_select_db($database, $this->link);
  97.  
  98.         if (!$selected) {
  99.             die("MySQL::selectDatabase() failed -- the database couldn't be selected: ".mysql_error());
  100.         }
  101.     }
  102. }
  103.  
  104. ?>
Add Comment
Please, Sign In to add comment