Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DataBaseAction{
- private $tableName ;
- public $tableId ;
- private $fieldNames = array() ;
- private $fieldType = array() ;
- private $valueArray = array();
- //connect to database properties
- public $host = "";
- public $dbName = "";
- public $dbUsername = "";
- public $dbPassword = "";
- public $conection ;
- function establishConnection(){
- $con = mysqli_connect($this->host, $this->dbUsername, $this->dbPassword, $this->dbName ) ;
- if(!$con)
- die("error connecting to database ");
- $this->conection = $con;
- }
- public function setValuesArray($array){
- $this->valueArray = $array;
- }
- // FIRST WE GOING TO FETCH FEILD NAMES AND STORE THEM INTO AN ARRAY
- private function fetchFeildNames(){
- if(!isset($this->tableName) && $this->tableName == null)
- exit("you must provide table name");
- $query = mysqli_query($this->conection , "SELECT * FROM $this->tableName ") ;
- if($query === false){
- exit( "Error in fetching tables fields info");
- }
- while ($fieldInfo = mysqli_fetch_field($query)) {
- $feildNameArray[] = $fieldInfo->name;
- $feildTypeArray[] = $fieldInfo->type;
- }
- $this->fieldNames = $feildNameArray;
- $this->fieldType = $feildTypeArray;
- }
- public function setTableName($name){
- $this->tableName = $name ;
- }
- public function insert(){
- $this->fetchFeildNames();
- //changing the field names from array to string to use it in the query
- $fieldNames = implode(",", $this->fieldNames);
- //prepearinf the question
- $values="";
- for($i=0;$i<count($this->fieldNames);$i++){
- $values .= "?,";
- }
- $values = substr( $values , 0, -1);
- //prepearing the statment
- $stmt = mysqli_prepare($this->conection,"INSERT INTO $this->tableName ($fieldNames) VALUES ($values)");
- if($stmt === false) {
- echo "ERROR :'(";
- exit;
- }
- //prepearing binding results;
- $typs="";
- for($i=0;$i<count($this->fieldType);$i++){
- switch ($this->fieldType[$i]) {
- case 3:
- $typs .="i";
- break;
- case 253:
- $typs .="s";
- break;
- case 252:
- $typs .="s";
- break;
- }
- }
- $finval = array ();
- $finval[]= $typs;
- for($i=0;$i<count($this->valueArray);$i++){
- $finval[] = & $this->valueArray[$i];
- }
- call_user_func_array(array($stmt, 'bind_param'),$finval);
- mysqli_stmt_execute($stmt);
- }
- //function below to update table in database
- public function update(){
- $this->fetchFeildNames();
- $text = "";
- for($i=1;$i<count($this->fieldNames);$i++){
- $text .= "`".$this->fieldNames[$i]."` = ? ,";
- }
- $text = substr($text, 0 ,-1);
- $sql="UPDATE $this->tableName SET $text WHERE $this->tableName.`id` = ? ";
- $stmt = mysqli_prepare($this->conection , $sql );
- //prepearing binding results;
- $typs="";
- for($i=1;$i<count($this->fieldType);$i++){
- switch ($this->fieldType[$i]) {
- case 3:
- $typs .="i";
- break;
- case 253:
- $typs .="s";
- break;
- case 252:
- $typs .="s";
- break;
- }
- }
- $typs .="i";
- $finval = array ();
- $finval[]= $typs;
- for($i=0;$i<count($this->valueArray);$i++){
- $finval[] = & $this->valueArray[$i];
- }
- $finval[] = & $this->tableId;
- call_user_func_array(array($stmt, 'bind_param') , $finval );
- mysqli_stmt_execute($stmt);
- }
- }
Add Comment
Please, Sign In to add comment