Guest User

Untitled

a guest
Aug 17th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.10 KB | None | 0 0
  1. <?php
  2.  
  3. class mysql extends DAL {
  4.    
  5.     //Class Variables
  6.     public $Host;
  7.     public $User;
  8.     public $pass;
  9.     public $dbname;
  10.    
  11.     function __construct($Host, $User, $Pass, $dbname, $query = NULL, $mode = NULL, $errorlocation = NULL) {
  12.         $this->Host = $Host;
  13.         $this->User = $User;
  14.         $this->pass = $Pass;
  15.         $this->dbname = $dbname;
  16.  
  17.         $this->connect();
  18.  
  19.         if($mode == 'row' && $query) {
  20.             $this->result = $this->fetch_row($query);
  21.         }
  22.    
  23.         elseif($mode == 'rows' && $query) {
  24.             $this->result = $this->fetch_rows($query);
  25.         }
  26.        
  27.         elseif($mode == NULL && $query) {
  28.             $this->query($query);
  29.         }
  30.     }
  31.  
  32.     public function connect() {
  33.         $this->set_last_error(null);
  34.         $this->db = new mysqli('p:' . $this->Host,$this->User, $this->pass, $this->dbname);
  35.  
  36.         if (!$this->db) {
  37.            
  38.            $this->set_last_error('Could not connect to database');
  39.         }
  40.    
  41.     }
  42.    
  43.     public function fetch_rows($query) {
  44.         //process params
  45.         $query = $this->process_params($query);
  46.         $this->set_last_error(null);
  47.        
  48.         $result = $this->db->query($query);
  49.  
  50.         $all = array();
  51.         if($result) {
  52.             while ($row = $result->fetch_assoc()) {
  53.                 $all[] = $row;
  54.             }  
  55.         }
  56.         return $all;
  57.        
  58.     }
  59.  
  60.     public function query($query) {
  61.         $this->set_last_error(null);
  62.         //process params
  63.         $query = $this->process_params($query);
  64.        
  65.        
  66.         $result = $this->db->query($query);
  67.     }
  68.  
  69.  
  70.     public function fetch_row($query) {
  71.         //process params
  72.        
  73.         $query = $this->process_params($query);
  74.         $this->set_last_error(null);
  75.        
  76.         if($result = $this->db->query($query)) {
  77.            
  78.         }
  79.        
  80.         else {
  81.             $this->set_last_error('Could not run query: ' . $this->db->error);
  82.         }
  83.  
  84.         return $result->fetch_row();
  85.     }
  86.  
  87.     public function get_column_names($table) {
  88.         $query = "SELECT * FROM `" . $table . "`";
  89.  
  90.         $result = $this->db->query($query);
  91.  
  92.         $row = $result->fetch_assoc();
  93.      
  94.         $columns = array_keys($row);
  95.         return $columns;
  96.     }
  97.  
  98.     function get_primary_key($table) {
  99.         $result = $this->db->query("SHOW KEYS FROM `$table` WHERE Key_name = 'PRIMARY'");
  100.         echo $table;
  101.         var_dump($result);
  102.         $row = $result->fetch_row();
  103.         return $row[4];
  104.     }
  105. }
Add Comment
Please, Sign In to add comment