Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.72 KB | None | 0 0
  1. <?php
  2. /************\
  3. | Class: Sql |
  4. \************/
  5.  
  6. class Sql
  7.   {
  8.   // Variables
  9.   public $handle = '';
  10.   public $type = '';
  11.   public $prefix = '';
  12.   public $database = '';
  13.  
  14.   // Constructor
  15.   //  Usage: $OBJECT_VAR = new Sql(TYPE, USERNAME, PASSWORD, SERVER, PREFIX[, DATABASE]);
  16.   public function __construct($sql_type = "mysql", $sql_user = "root", $sql_pass = "", $sql_src = "localhost", $sql_prefix = "", $sql_base = null)
  17.     {
  18.     $this->type = $sql_type;
  19.     $this->prefix = $sql_pref;
  20.     $this->database = $sql_base;
  21.    
  22.     if ($sql_type == "mysql")
  23.       $this->handle = mysql_connect($sql_host, $sql_user, $sql_pass);
  24.     elseif ($sql_type == "pgsql")
  25.       $this->handle = pg_connect("host=$sql_host user=$sql_user password=$sql_pass");
  26.     elseif ($sql_type == "sqlite")
  27.       $this->handle = sqlite_open($sql_base, 0666, $error);
  28.     }
  29.  
  30.   // Query
  31.   //  Usage: $OBJECT_VAR->query(QUERY);
  32.   public function query($query)
  33.     {
  34.     $this->focus($this->database);
  35.     if ($this->type == "mysql")
  36.       return mysql_query($query, $this->handle);
  37.     elseif ($this->type == "pgsql")
  38.       return pg_query($this->handle, $query);
  39.     elseif ($this->type == "sqlite")
  40.       return sqlite_query($this->handle, $query);
  41.     }
  42.  
  43.   // Query (alias)
  44.   //  Usage: $OBJECT_VAR->q(QUERY);
  45.   public function q($query)
  46.     {
  47.     return $this->query($query);
  48.     }
  49.  
  50.   // SQL Select Database
  51.   //  Usage: $OBJECT_VAR->focus(DATABASE_NAME);
  52.   public function focus($database)
  53.     {
  54.     if ($this->type == "mysql")
  55.       return mysql_select_db($database, $this->handle);
  56.     elseif ($this->type == "sqlite")
  57.       return true;
  58.     else
  59.       return mysql_query("USE `$database`", $this->handle);
  60.     }
  61.  
  62.   // SQL Result Resource to Array
  63.   //  Usage: $OBJECT_VAR->r2Array(RESOURCE);
  64.   public function r2Array($result)
  65.     {
  66.     if ($this->type == "mysql")
  67.       while ($array[] = mysql_fetch_assoc($result));
  68.     if ($this->type == "pgsql")
  69.       while ($array[] = pg_fetch_assoc($result));
  70.     elseif ($this->type == "sqlite")
  71.       return sqlite_fetch_array($result, SQLITE_ASSOC);
  72.     array_pop($array);
  73.     return $array;
  74.     }
  75.  
  76.   // SQL Table to Array
  77.   //  Usage: $OBJECT_VAR->t2Array(TABLE);
  78.   public function t2Array($table)
  79.     {
  80.     $result = query("SELECT * FROM `$table`");
  81.     $array = r2array($result);
  82.     return $array;
  83.     }
  84.  
  85.   // SQL Number of Rows
  86.   //  Usage: $OBJECT_VAR->num_rows(RESOURCE);
  87.   public function num_rows($resource)
  88.     {
  89.     if ($this->type == "mysql")
  90.       return mysql_num_rows($resource);
  91.     if ($this->type == "pgsql")
  92.       return pg_num_rows($resource);
  93.     if ($this->type == "sqlite")
  94.       return sqlite_num_rows($resource);
  95.     }
  96.  
  97.   // SQL Escape String
  98.   //  Usage: $OBJECT_VAR->es(STRING);
  99.   public function es($string)
  100.     {
  101.     if ($this->type == "mysql")
  102.       return mysql_real_escape_string($string);
  103.     elseif ($this->type == "pgsql")
  104.       return pg_escape_string($string);
  105.     else
  106.       return addslashes($string);
  107.     }
  108.  
  109.   // SQL Close
  110.   //  Usage: $OBJECT_VAR->close(RESOURCE);
  111.   public function close($resource = null)
  112.     {
  113.     if ($this->type == "mysql")
  114.       return mysql_close($resource);
  115.     elseif ($this->type == "pgsql")
  116.       return pg_close($resource);
  117.     elseif ($this->type == "sqlite")
  118.       return sqlite_close($resource);
  119.     }
  120.  
  121.   // SQL Error
  122.   //  Usage: $OBJECT_VAR->error(RESOURCE);
  123.   public function error($result = null)
  124.     {
  125.     if ($this->type == "mysql")
  126.       return mysql_error($result);
  127.     elseif ($this->type == "pgsql")
  128.       return pg_result_error($result);
  129.     elseif ($this->type == "sqlite")
  130.       return sqlite_error_string(sqlite_last_error($result));
  131.     }
  132.   }
  133.  
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement