Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.95 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @author      profusion
  5.  * @copyright   2011
  6.  */
  7.  
  8. class MySQL {
  9.    
  10.     /**
  11.      * @name    linkID
  12.      * @var     resource
  13.      * @access  private
  14.      */
  15.     private $linkID = null;
  16.    
  17.     /**
  18.      * @name    dbHost
  19.      * @var     string
  20.      * @access  private
  21.      */
  22.     private $dbHost;
  23.    
  24.     /**
  25.      * @name    dbUser
  26.      * @var     string
  27.      * @access  private
  28.      */
  29.     private $dbUser;
  30.    
  31.     /**
  32.      * @name    dbPass
  33.      * @var     string
  34.      * @access  private
  35.      */
  36.     private $dbPass;
  37.    
  38.     /**
  39.      * @name    dbName
  40.      * @var     string
  41.      * @access  private
  42.      */
  43.     private $dbName;
  44.    
  45.     /**
  46.      * @name    lastSQL
  47.      * @var     resource
  48.      * @access  private
  49.      */
  50.     private $lastSQL = null;
  51.    
  52.     /**
  53.      * @name    __construct
  54.      * @param   dbHost
  55.      * @param   dbUser
  56.      * @param   dbPass
  57.      * @param   dbName
  58.      * @access  public
  59.      * @return  void
  60.      */
  61.     public function __construct($dbHost, $dbUser, $dbPass, $dbName) {
  62.         $this->dbHost = (!empty($dbHost) ? $dbHost : 'localhost');
  63.         $this->dbUser = $dbUser;
  64.         $this->dbPass = $dbPass;
  65.         $this->dbName = $dbName;
  66.        
  67.         $this->connect();
  68.     }
  69.    
  70.     /**
  71.      * @name    connect
  72.      * @access  private
  73.      * @return  void
  74.      */
  75.     private function connect() {
  76.         if (false == $this->linkID = @mysql_connect($this->dbHost, $this->dbUser, $this->dbPass)) {
  77.             $this->error('Unable to connect server "'.$this->dbHost.'" with username "'.$this->dbUser.'"!');
  78.         } elseif (false == @mysql_select_db($this->dbName, $this->linkID)) {
  79.             $this->error('Unable to connect database "'.$this->dbName.'"!');
  80.         }
  81.     }
  82.    
  83.     /**
  84.      * @name    sendQuery
  85.      * @param   query
  86.      * @access  public
  87.      * @return  mixed
  88.      */
  89.     public function sendQuery($query) {
  90.         if (false == $queryResult = @mysql_query($query)) {
  91.             $this->error('Unable to execute query "'.$query.'"!');
  92.         } else {
  93.             return $this->lastSQL = $queryResult;
  94.         }
  95.     }
  96.    
  97.     /**
  98.      * @name    fetch
  99.      * @param   query
  100.      * @access  public
  101.      * @return  mixed
  102.      */
  103.     public function fetch($query = '') {
  104.         $query = (empty($query) ? $this->lastSQL : $query);
  105.         $query = (!is_resource($query) ? $this->sendQuery($query) : $query);
  106.        
  107.         $array = @mysql_fetch_assoc($query);
  108.         $this->freeResult($query);
  109.        
  110.         return $array;
  111.     }
  112.    
  113.     /**
  114.      * @name    numRows
  115.      * @param   result
  116.      * @access  public
  117.      * @return  mixed
  118.      */
  119.     public function numRows($result = '') {
  120.         return @mysql_num_rows((!empty($result) ? $result : $this->lastSQL));
  121.     }
  122.    
  123.     /**
  124.      * @name    insert
  125.      * @param   data
  126.      * @param   table
  127.      * @access  public
  128.      * @return  mixed
  129.      */
  130.     public function insert($data, $table) {
  131.         $cols = $keys = array();
  132.        
  133.         foreach ($data as $col => $key) {
  134.             $key = (is_int($key) || is_numeric($key) ? (int)$key : $this->escapeString($key));
  135.            
  136.             $cols[] = '`'.$col.'`';
  137.             $keys[] = '\''.$key.'\'';
  138.         }
  139.        
  140.         // preparing sql
  141.         $sql = 'INSERT INTO `'.$table.'` ';
  142.         $sql .= '('.implode(', ', $cols).')';
  143.         $sql .= ' VALUES ';
  144.         $sql .= '('.implode(', ', $keys).');';
  145.        
  146.         return $this->sendQuery($sql);
  147.     }
  148.    
  149.     /**
  150.      * @name    freeResult
  151.      * @param   result
  152.      * @access  public
  153.      * @return  mixed
  154.      */
  155.     public function freeResult($result) {
  156.         @mysql_free_result($result);
  157.     }
  158.    
  159.     /**
  160.      * @name    escapeString
  161.      * @param   string
  162.      * @access  public
  163.      * @return  string
  164.      */
  165.     public function escapeString($string) {
  166.         return mysql_real_escape_string(addslashes($string), $this->linkID);
  167.     }
  168.    
  169.     /**
  170.      * @name    error
  171.      * @param   text
  172.      * @access  private
  173.      * @return  void
  174.      */
  175.     private function error($text) {
  176.         die('<h1>MySQL Class Error:</h1> '.$text.'<br /><hr />MySQL Error: '.mysql_error().'<br /> MySQL Errno: '.mysql_errno());
  177.     }
  178.    
  179.     /**
  180.      * @name    __destruct
  181.      * @access  public
  182.      * @return  void
  183.      */
  184.     public function __destruct() {
  185.         @mysql_close($this->linkID);
  186.     }
  187.    
  188. }
  189.  
  190. // new object
  191. $MySQL = new MySQL('localhost', 'root', '', 'database');
  192.  
  193. // insert
  194. $MySQL->insert(array(
  195.                     // col        // value
  196.                     'userName' => 'hallo123'
  197.                     ), 'users');
  198.  
  199. // fetching a query
  200. $MySQL->sendQuery('SELECT `userName` FROM `users` WHERE `userName` = '.$this->escapeString($_GET['userName']).';');
  201.  
  202. // rows
  203. $rows = $MySQL->numRows();
  204.  
  205. // row
  206. $row = $MySQL->fetch();
  207.  
  208. /*
  209. echo 'rows: '.$rows;
  210. echo 'username: '.$row['userName'];
  211. */
  212. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement