<?php
session_start();
if(isset($_POST["paramValues"])){
    $paramObj=json_decode($_POST["paramValues"]);
    $paramObj->userEmpId=$_SESSION["userId"];
    $paramObj->department=$_SESSION["teamLeads"];
    $clsObj= new AllPhpFetchMethods();
    $clsObj->retreivingDataFromDataBase($paramObj);
}else{
    echo "result not found";
}
class AllPhpFetchMethods{
    function __construct(){
        require_once dirname(__DIR__).'../../../php/DB_Connect.php';
        $clsObj = new DB_conncect();
        $this->db=$clsObj->connectToDB();
        $this->db_Pdo=$clsObj->connectToDBVia_PDO();
    }
    
    function retreivingDataFromDataBase($paramObj){
        $this->prepareStatement($paramObj);  // 1)
        $this->queryStatement($paramObj);   // 2)
    }
    
    // -----------------------------------------------------------------------------
    function prepareStatement($paramObj){    //1)   // prepare statement  not allows multiple select querys 
        $this->prepareStmt_MYSQLI_Statement($paramObj); //a)
        $this->prepareStmt_PDO_Statement($paramObj);   //b)
    }
    
    function queryStatement($paramObj){      //2) // query statement  allows multiple select querys and (not allowed "bind_params, $stmt->execute() , get_result())
        $this->queryStmt_MYSQLI_Statement($paramObj); //a)
        $this->queryStmt__PDO_Statement($paramObj);    //b)
    }
    
    
    
    
    //----------------------------- PrepareStateMents-----------------------------------
    function prepareStmt_MYSQLI_Statement($paramObj){    //1)
        $resultArray=array();
        $temp=array();
        // $stmt= $this->db->prepare("SELECT `EMP_ID`,`EMP_NAME` FROM `empCircle` WHERE `EMP_ID`=?");
        // $stmt->bind_param("s",$paramObj->userEmpId);
        // $stmt->execute();
        // $stmt->bind_result($empId, $empName);
        // $stmt->store_result();
        // if($stmt->num_rows()>0){
        //     while($stmt->fetch()){ // is next data exisit (true/false)
        //         // type 1
        //         $temp=array(
        //             "empId"=>$empId,
        //             "empName"=>$empName
        //             );
        //         // or  we can use  type2
        //         $temp["empId"]=$empId;
        //         $temp["empName"]=$empName;
        //         array_push($resultArray, $temp);
        //     }
            
            
            
        // }
        
        $stmt2=$this->db->prepare("SELECT `EMP_ID`, `EMP_NAME` FROM `empCircle` WHERE `EMP_ID`=?");
        $stmt->bind_param("s",$paramObj->userEmpId);
        $stmt2->execute();
        $result=$stmt2->get_result();
        
        // while($data=$result->fetch_array()){ //numbers, assoc Array
        //     array_push($temp, $data);
        // }
        // $resultArray["fetch_array"]=$temp;
        // unset($temp);
        
        // while($data=$result->fetch_row()){  // numeric
        //     array_push($temp,$data);
        // }
        // $resultArray["fetch_row"]=$temp;
        // unset($temp);
        
        // while($data=$result->fetch_assoc()){    // asso array
        //     array_push($temp, $data);
        // }
        // $resultArray["fetch_assoc"]=$temp;
        // unset($temp);
        
        // while($data=$result->fetch_object()){ // object 
        //     array_push($temp, $data);
        // }
        // $resultArray["fetchObjec"]=$temp;
        // unset($temp);
        
        // while($data=$result->fetch_field()){
        //     array_push($temp,$data);
        // }
        // $resultArray["fetch_field"]=$temp;
        // unset($temp);
        
        
        $resultArray["fetchAll"]=$result->fetch_all();
        unset($temp);
        
        // $resultArray["fetchArray"]=$result->fetch_array();
        // $resultArray["fetchAssoc"]=$result->fetch_assoc();
        // $resultArray["fetchRow"]=$result->fetch_row();
        // $resultArray["fetchObject"]=$result->fetch_object();
        // $resultArray["fetchField"]=$result->fetch_field(); // get column complete details eg:(columnName, DbName, columnLength, tableName)
        // $resultArray["fetchall"]=$result->fetch_all();
        // $resultArray["fetch"]=$stmt2->fetch();
        
        
        echo json_encode($resultArray);
    }
    
    function prepareStmt_PDO_Statement($paramObj){    //1)
        $resultArray=array();
        $temp=array();
        $stmt= $this->db_Pdo->prepare("SELECT `EMP_ID`,`EMP_NAME` FROM `empCircle` WHERE `EMP_ID`=?");
        $stmt->bindParam(1,$paramObj->userEmpId, PDO::PARAM_INT);
        //$stmt->bindParam(2,$paramObj->userEmpId, PDO::PARAM_STR);
        $stmt->execute();
        // $stmt->bind_result($empId, $empName); You do not need an ugly bind_result with PDO at all.
        if($stmt->rowCount()>0){
             while($data=$stmt->fetch(PDO::FETCH_BOTH)){  //FETCH_BOTH
                array_push($temp,$data);
            }
            $resultArray["fetchBoth"]=$temp;
            unset($temp);
            
            // while($data=$stmt->fetch(PDO::FETCH_LAZY)){  //FETCH_LAZY
            //     array_push($temp, $data);
            // }
            // $resultArray["fetchLazy"]=$temp;
            // unset($temp);
            
            // while($data=$stmt->fetch(PDO::FETCH_ASSOC)){  //FETCH_ASSOC
            //     array_push($temp, $data);
            // }
            // $resultArray["fetchAssoc"]=$temp;
            // unset($temp);
            
            // while($data=$stmt->fetch(PDO::FETCH_OBJ)){ // FETCH_OBJECT
            //     array_push($temp, $data);
                
            // }
            // $resultArray["fetchObj"]= $temp;
            // unset($temp);
            
            // while($data=$stmt->fetchColumn()){ // fetchColumn() only return each loop first colums
            //     array_push($temp,$data);
            // }
            // $resultArray["fectcolumn"]=$temp;
            
            //  while($data=$stmt->fetchColumn(1)){ // fetchColumn() only return each loop secon columns
            //     array_push($temp,$data);
            // }
            // $resultArray["fectcolumn"]=$temp;
            
            // while($data=$stmt->fetchObject()){
            //     array_push($temp, $data);
            // }
            // $resultArray["fetchObject"]=$temp;
            
            // while($data=$stmt->fetch(PDO::FETCH_NUM)){ // FETCH_OBJECT
            //     array_push($temp, $data);
                
            // }
            // $resultArray["fetchNum"]= $temp;
            // unset($temp);
            
            // $resultArray["fetchBoth"]=$stmt->fetch(PDO::FETCH_BOTH); // fetch_array[numbers, associative]
            // $resultArray["fetchLazy"]=$stmt->fetch(PDO::FETCH_LAZY); // associate array along with  queryString="complete query"
            // $resultArray["fetchAssoc"]=$stmt->fetch(PDO::FETCH_ASSOC); // associate array
            // $resultArray["fetchObj"]=$stmt->fetch(PDO::FETCH_OBJ);// object
            // $resultArray["fetchAll"]=$stmt->fetchAll(); // all data in single line
            // $resultArray["fectcolumn"]=$stmt->fetchColumn();1,2,.....
            //$resultArray["fetchObject"]=$stmt->fetchObject();  as same as fetch(PDO::FETCH_OBJ)
            //$resultArray["fetchNum"]=$stmt->fetch(PDO:: FETCH_NUM)// only value  return not key eg:("name"=>"datta","id"=>"200403") output:("datta","200403")
        }
        echo json_encode($resultArray);
        
    }
    
    // -----------------------------------------end PrepareStatement ------------------------------------------------
    
    
    //-------------------------------------query statement Start-------------------------------------
    
    function queryStmt_MYSQLI_Statement($paramObj){
        
        //---------------single query----------------------
        
        // $resultArray=array();
        // $stmt=$this->db->query("SELECT * FROM `empCircle`");
        // // not allowed "bind_params, $stmt->execute() , get_result()
        // $resultArray["fetchArray"]=$stmt->fetch_array();
        // $resultArray["fetchRow"]=$stmt->fetch_row();
        // $resultArray["fetchAssociate"]=$stmt->fetch_assoc();
        // $resultArray["fetchObject"]=$stmt->fetch_object();
        // $resultArray["fetchField"]=$stmt->fetch_field();
        // $resultArray["fectAll"]=$stmt->fetch_all();
        
        // echo json_encode($resultArray);
        
        
        
        //------------------multi query---------------------
        
        
        $resultArray=array();
        $queryTitleArray=["fnAdvanceTableData", "empCircleTableData"];
        $query1="SELECT * FROM `fn_advances`;";
        $query2=" SELECT * FROM `empCircle`";
        $this->db->multi_query($query1.$query2);
        $index=0;
            do{
                $temp=array();
                $result=$this->db->store_result();
                while($data=$result->fetch_row()){      // also use " fetch_array(),fetch_assoc(), fetch_object(),fetch_all()
                    array_push($temp, $data);
                }
                
                $resultArray[$queryTitleArray[$index]]=$temp;
                $index++;
            }while($this->db->next_result());
            
        echo json_encode($resultArray);
    }
    
    
     function queryStmt__PDO_Statement($paramObj){
        
        //---------------single query----------------------
        // $resultArray=array();
        // $temp=array();
        // $stmt=$this->db_Pdo->query("SELECT * FROM `empCircle`");
        
        //  while($data=$stmt->fetch(PDO::FETCH_BOTH)){ //array(numaric, assoc)
        //     array_push($temp, $data);
        // }
        // $resultArray["fetchBoth"]=$temp;
        // unset($temp);
        
        // while($data=$stmt->fetch(PDO::FETCH_LAZY)){  // numaric
        //     array_push($temp, $data);
        // }
        // $resultArray["fetch lazy"]=$temp;
        // unset($temp);
        
        // $resultArray["fetchAssoc"]=$stmt->fetch(PDO::FETCH_ASSOC);
        // $resultArray["fetchObj"]=$stmt->fetch(PDO:: FETCH_OBJ);
        // $resultArray["fetchNum"]=$stmt->fetch(PDO :: FETCH_NUM);
        // $resultArray["fetchColumn"]=$stmt->fetchColumn(2);// 2 is column number
        // $resultArray["fetchObject"]=$stmt->fetchObject();
        // $resultArray["fetchAll"]=$stmt->fetchAll();
        
        // echo json_encode($resultArray);
        
        
        
        //------------------multi query---------------------
        
        
        $resultArray=array();
        $queryTitleArray=["fnAdvanceTableData", "empCircleTableData"];
        $query1="SELECT * FROM `fn_advances`;";
        $query2=" SELECT * FROM `empCircle`";
        $stmt=$this->db_Pdo->query($query1.$query2);
        $index=0;
            do{
                $temp=array();
                // $result=$this->db->store_result();
                while($data=$stmt->fetch(PDO::FETCH_ASSOC)){ //also use "(PDO::FETCH_BOTH),(PDO:: FETCH_LAZY), (PDO:: FETCH_NUM), fetchColumn() ,fetchObject
                    array_push($temp, $data);
                }
                
                $resultArray[$queryTitleArray[$index]]=$temp;  // also use directly without while loop fetchAll()
                $index++;
            }while($stmt->nextRowset());
            
        echo json_encode($resultArray);
    }
    
    
}


?>