Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.78 KB | None | 0 0
  1. <?php
  2.     class mysql{
  3.         var $dbhost=DB_HOST;
  4.         var $dbuser=DB_USER ;
  5.         var $dbpass=DB_PASS ;
  6.         var $dbase=DB_NAME;
  7.         var $link;
  8.         var $result;
  9.         var  $nr_rows;
  10.         var $sql;
  11.         var $affected;
  12.         var $count;
  13.         function debug(){
  14.             echo $this->sql;
  15.             echo mysql_errno($this->link)." - ".mysql_error($this->link);
  16.             die();
  17.         }
  18.         function connect(){
  19.             if (empty($this->link)){
  20.                 $this->link = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die("Unable to connect") ;
  21.                 mysql_select_db($this->dbase,$this->link) or die ("Unable to select DB");
  22.             }
  23.         }
  24.         function disconect(){
  25.             mysql_close($this->link);
  26.    
  27.         }
  28.         function query($query = "" ){
  29.             $this->count++;
  30.             if (!$this->link) $this->connect();
  31.             if (!empty($query)) $this->sql = $query;
  32.             if(!empty($this->sql)){
  33.                 if (!($this->result = @mysql_query($this->sql))){
  34.                     $this->debug();
  35.                 } else {
  36.                     //$this->nr_rows =mysql_num_rows($this->result);
  37.                 }
  38.                 //return $this->result;
  39.    
  40.             }
  41.    
  42.         }
  43.         function insert($table,$ArrayFieldsAndValues){
  44.             $fields = array();
  45.             $values = array();
  46.             $this->sql = "INSERT INTO $table (";
  47.             foreach ($ArrayFieldsAndValues as $key=>$val){
  48.                 $fields[] = $key;
  49.                 $values[] = $val;
  50.             }
  51.    
  52.             foreach ($fields as $field)
  53.             $this->sql .=$field ." ,";
  54.             $this->sql{strlen($this->sql)-1} = ")";
  55.             $this->sql.=" VALUES (";
  56.    
  57.    
  58.    
  59.             foreach ($values as $val)
  60.             $this->sql .="'".$val ."',";
  61.             $this->sql{strlen($this->sql)-1} = ")";
  62.    
  63.    
  64.             $this->query();
  65.         }
  66.         function __getAll(){
  67.             $this->query();
  68.             $r = array();
  69.             while ($rb = mysql_fetch_assoc($this->result)){
  70.                 $r[] = $rb;
  71.             };
  72.             return $r;
  73.         }
  74.         function getAll($sql = ""){
  75.             if(!empty($sql)){
  76.                 $this->sql = $sql;
  77.                 $this->query();
  78.             }
  79.             return $this->__getAll();
  80.         }
  81.         function getRow($sql = ""){
  82.             if(!empty($sql)){
  83.                 $this->sql = $sql;
  84.                 $this->query();
  85.             }
  86.             $r = mysql_fetch_assoc($this->result);
  87.             return $r;
  88.         }
  89.         function getColumns($table){
  90.             $this->query("DESCRIBE `$table`");
  91.             $columns=array();
  92.             foreach ($this->getAll() as $v) {
  93.                 $columns[]=$v['Field'];
  94.             }
  95.             return $columns;
  96.         }
  97.         function update($table,$Array,$condition){
  98.             $compose = array();
  99.             $Array=$this->escape_array($Array);
  100.             if (is_array($this->getColumns($table))){
  101.                 foreach ($this->getColumns($table) as $key){
  102.                     $val = $Array[$key];
  103.                     if (!empty($val)) {
  104.                         $compose[] = "`$key`" ."= '".$val."'";
  105.                     }
  106.                 }
  107.             }
  108.             $this->sql = "UPDATE `$table` SET " . implode(", ",$compose)." WHERE $condition";
  109.             $this->query($this->sql);
  110.         }
  111.         function escape($string){
  112.             $this->connect();
  113.             if(!is_array($string))
  114.             return mysql_real_escape_string($string,$this->link);
  115.         }
  116.         function escape_array($array) {
  117.             foreach ($array as $k=>$v) {
  118.                 $array[$k] = escape_data($v);
  119.             }
  120.             return $array;
  121.         }
  122.         function last_id(){
  123.             return mysql_insert_id($this->link);
  124.         }
  125.         function num_rows($sql=''){
  126.             if (!$this->link) $this->connect();
  127.             if (!empty($sql)) $this->sql = $sql;
  128.             $this->query();
  129.             return mysql_num_rows($this->result);
  130.    
  131.         }
  132.         function disconnect(){
  133.             if($this->link) mysql_close($this->link);
  134.         }
  135.     }
  136. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement