Advertisement
Khadafi995

Table class

Aug 25th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. <?php
  2. /** Class table **/
  3. include_once("db.php");
  4.  
  5. class DbTable{
  6.     private $table_name;
  7.     private $query;
  8.     private $db;
  9.    
  10.     function __construct($table_name){
  11.         $this->db = new DataBase();
  12.         $this->table_name = $table_name;
  13.     }
  14.    
  15.     /**
  16.     Prepare and execute query
  17.    
  18.     @param query SQL
  19.     @param query_array array
  20.     **/
  21.     function _query($query, $query_array = ""){
  22.         try{
  23.             $this->db->query("SET NAMES UTF8");
  24.             if ($query_array != ""){
  25.                 $rqq = $this->db->prepare($query);
  26.                 $rqq->execute(array($query_array));
  27.             }
  28.             else{
  29.                 $this->db->query($query);
  30.             }
  31.             return true;
  32.         }
  33.         catch(Exception $e){
  34.             die("Error : " . $e->getMessage());
  35.             return false;
  36.         }
  37.        
  38.     }
  39.    
  40.     /**
  41.     Show spécific item
  42.    
  43.     @param item_name string
  44.     **/
  45.     function show_item($item_name){
  46.         $rqq = "SELECT ".$item_name." FROM ".$this->table_name;
  47.         $found_item = $this->_query($rqq);
  48.         return $found_item;
  49.     }
  50.    
  51.     /**
  52.     Add item
  53.    
  54.     @param item_attr string
  55.     @param item_val string
  56.     **/
  57.     function add_item($item_attr="", $item_val=""){
  58.         $rqq = "INSERT INTO " . $this->table_name . "(" . $item_attr . ") VALUES (".$item_val.")";
  59.         try{
  60.             $this->_query();
  61.         }
  62.         catch(Excepetion $e){
  63.             die('Error : '.$e->getMessage());
  64.             return false;
  65.         }
  66.     }
  67.     function update_item(){
  68.         // WIP
  69.     }
  70.     function remove_item(){
  71.         // WIP
  72.     }
  73.     function list_item(){
  74.         $rqq = "SELECT * FROM ".$this->table_name;
  75.         $list_item = $this->query($rqq);
  76.         return $list_item;
  77.     }
  78. }
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement