SHARE
TWEET

Untitled

a guest Aug 14th, 2016 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class DataBaseAction{
  3.  
  4.     private $tableName      ;
  5.     public  $tableId    ;
  6.     private $fieldNames = array() ;
  7.     private $fieldType  = array() ;
  8.     private $valueArray = array();
  9.  
  10.  
  11.     //connect to database properties
  12.     public $host       = "";
  13.     public $dbName     = "";
  14.     public $dbUsername = "";
  15.     public $dbPassword = "";
  16.     public $conection ;
  17.  
  18.  
  19.     function establishConnection(){
  20.         $con = mysqli_connect($this->host, $this->dbUsername, $this->dbPassword, $this->dbName ) ;
  21.         if(!$con)
  22.             die("error connecting to database ");
  23.             $this->conection = $con;
  24.  
  25.     }
  26.     public function setValuesArray($array){
  27.         $this->valueArray = $array;
  28.     }
  29.  
  30. // FIRST WE GOING TO FETCH FEILD NAMES AND STORE THEM INTO AN ARRAY
  31.     private function fetchFeildNames(){
  32.         if(!isset($this->tableName) && $this->tableName == null)
  33.             exit("you must provide table name");
  34.        $query = mysqli_query($this->conection ,  "SELECT * FROM $this->tableName ") ;
  35.        if($query === false){
  36.            exit( "Error in fetching tables fields info");
  37.        }
  38.        while ($fieldInfo = mysqli_fetch_field($query)) {
  39.             $feildNameArray[] = $fieldInfo->name;
  40.             $feildTypeArray[] = $fieldInfo->type;  
  41.         }
  42.         $this->fieldNames = $feildNameArray;
  43.         $this->fieldType  = $feildTypeArray;
  44.  
  45.     }
  46.  
  47.     public function setTableName($name){
  48.         $this->tableName = $name ;
  49.     }
  50.     public function insert(){
  51.        $this->fetchFeildNames();
  52.        //changing the field names from array to string to use it in the query
  53.  
  54.        $fieldNames = implode(",", $this->fieldNames);
  55.        //prepearinf the question
  56.        $values="";
  57.        for($i=0;$i<count($this->fieldNames);$i++){
  58.             $values .= "?,";
  59.         }
  60.        $values = substr( $values , 0, -1);
  61.        //prepearing the statment              
  62.         $stmt = mysqli_prepare($this->conection,"INSERT INTO $this->tableName ($fieldNames) VALUES ($values)");
  63.        if($stmt === false) {
  64.             echo "ERROR :'(";
  65.             exit;
  66.         }
  67.  
  68.         //prepearing binding results;
  69.         $typs="";
  70.         for($i=0;$i<count($this->fieldType);$i++){
  71.             switch ($this->fieldType[$i]) {
  72.                 case 3:
  73.                   $typs .="i";
  74.                   break;
  75.                 case 253:
  76.                   $typs .="s";
  77.                    break;
  78.                 case 252:
  79.                   $typs .="s";
  80.                     break;
  81.             }
  82.         }
  83.         $finval = array ();
  84.         $finval[]=  $typs;
  85.         for($i=0;$i<count($this->valueArray);$i++){
  86.             $finval[] = & $this->valueArray[$i];
  87.         }
  88.         call_user_func_array(array($stmt, 'bind_param'),$finval);
  89.         mysqli_stmt_execute($stmt);
  90.     }
  91.     //function below to update table in database
  92.     public function update(){
  93.         $this->fetchFeildNames();
  94.         $text = "";
  95.         for($i=1;$i<count($this->fieldNames);$i++){
  96.             $text .= "`".$this->fieldNames[$i]."` = ? ,";
  97.         }
  98.         $text = substr($text, 0 ,-1);
  99.         $sql="UPDATE $this->tableName SET $text WHERE $this->tableName.`id` = ? ";
  100.  
  101.         $stmt = mysqli_prepare($this->conection , $sql );
  102.  
  103.          //prepearing binding results;
  104.         $typs="";
  105.         for($i=1;$i<count($this->fieldType);$i++){
  106.             switch ($this->fieldType[$i]) {
  107.                 case 3:
  108.                   $typs .="i";
  109.                   break;
  110.                 case 253:
  111.                   $typs .="s";
  112.                    break;
  113.                 case 252:
  114.                   $typs .="s";
  115.                     break;
  116.             }
  117.         }
  118.         $typs .="i";
  119.  
  120.         $finval = array ();
  121.         $finval[]=  $typs;
  122.         for($i=0;$i<count($this->valueArray);$i++){
  123.             $finval[] = & $this->valueArray[$i];
  124.         }
  125.  
  126.         $finval[] = & $this->tableId;
  127.  
  128.         call_user_func_array(array($stmt, 'bind_param') , $finval );
  129.         mysqli_stmt_execute($stmt);
  130.     }
  131.  
  132. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top