Guest User

Untitled

a guest
Oct 23rd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <?php
  2. Class DBCommon {
  3.     private $_host = 'localhost';
  4.     private $_user = 'root';
  5.     private $_pass = '';
  6.     private $_db = 'hotel';
  7.    
  8.     private $_link = null;
  9.     private $_link_db = null;
  10.     protected $_tableName = null;
  11.    
  12.     public function __construct() {
  13.         $this->connect();
  14.     }
  15.    
  16.     public function getTableName(){
  17.         return $this->_tableName;
  18.     }
  19.    
  20.     public function setTableName($name){
  21.         $this->_tableName = $name;
  22.     }
  23.    
  24.     public function getLink()
  25.     {
  26.         return $this->_link;
  27.     }
  28.    
  29.     /**
  30.     * Conecta ao banco de dados
  31.     *
  32.     * @return bool
  33.     */
  34.     public function connect() {
  35.         if($this->_link == null && $this->_link == false) {
  36.             $this->_link = mysql_connect($this->_host, $this->_user, $this->_pass);
  37.             $this->_link_db = mysql_select_db($this->_db);
  38.             return true;
  39.         } else {
  40.             if(!is_resource($this->_link)) {
  41.                 $this->_link = mysql_connect($this->_host, $this->_user, $this->_pass);
  42.                 $this->_link_db = mysql_select_db($this->_db);
  43.                 return true;
  44.             }
  45.         }
  46.     }
  47.    
  48.     /**
  49.      * Fecha uma conexao ao banco de dados
  50.      *
  51.      * @return bool
  52.      */
  53.     public function close(){
  54.         if($this->_link == null) {
  55.             return true;
  56.         } else {
  57.             if(is_resource($this->_link)) {
  58.                 return mysql_close($this->_link);
  59.             } else {
  60.                 return true;
  61.             }
  62.         }
  63.     }
  64.    
  65.     /**
  66.     * Insere um dado na tabela
  67.     *
  68.     * @param string $table tabela que irá receber os dados
  69.     * @param array $data dados que serão inseridos, onde a chave é o campo e o valor o dado a ser inserido
  70.     * @return bool
  71.     */
  72.     static public function insert($table = '', $data) {
  73.         if(!is_array($data) || $table == '') {
  74.             return false;
  75.         }
  76.    
  77.         if(count($data)==0) {
  78.             return false;
  79.         }
  80.         $fields = array();
  81.         $values = array();
  82.         foreach ($data as $field=>$value) {
  83.             $fields[] = $field;
  84.             if($value == 'NOW()') {
  85.                 $values[] = $value;
  86.             } else {
  87.                 $values[] = "'" . $value . "'";
  88.             }
  89.         }
  90.    
  91.         $this->connect();
  92.    
  93.         $strFields = implode(',', $fields);
  94.         $strValues = implode(',', $values);
  95.         $sql = 'INSERT INTO ' . $table . ' (' . $strFields . ') VALUES (' . $strValues . ');';
  96.    
  97.         $resource = mysql_query($sql, $this->_link);
  98.    
  99.         $id = mysql_insert_id($this->getLink());
  100.    
  101.         $this->close();
  102.    
  103.         return $id;
  104.     }
  105.    
  106.     static public function query($sql)
  107.     {
  108.         $link = $this->connect();
  109.         $rs = mysql_query($sql, $link);
  110.         $this->close();
  111.        
  112.         return $rs;
  113.     }
  114.    
  115.     static public function delete($table, $where)
  116.     {
  117.         $link = $this->connect();
  118.         $sql = 'DELETE FROM ' . $table . ' ' . $where;
  119.         $rs = mysql_query($sql, $link);
  120.         $this->close();
  121.        
  122.         return $rs;
  123.     }
  124.    
  125.     static public function fetch($sql, $type=1)
  126.     {
  127.         $types = array(
  128.             1 => MYSQL_ASSOC,
  129.             2 => MYSQL_NUM,
  130.             3 => MYSQL_BOTH
  131.         );
  132.        
  133.         if($types[$type]) {
  134.             $resultType = $types[$type];
  135.         } else {
  136.             $resultType = $types[3];
  137.         }
  138.        
  139.         $obj = new DBCommon();
  140.         $obj->connect();
  141.         $link = $obj->getLink();
  142.         $rs = mysql_query($sql, $link);
  143.         $data = array();
  144.         if($rs) {
  145.             if(mysql_num_rows($rs)) {
  146.                 while($row = mysql_fetch_array($rs, $resultType)) {
  147.                     $data[] = $row;
  148.                 }
  149.             }
  150.         } else {
  151.             return false;
  152.         }
  153.        
  154.         return $data;
  155.     }
  156. }
Add Comment
Please, Sign In to add comment