DarkChocolate Reborn
DarkChocolate Reborn

Reputation: 43

PHP echoed variable returns a mysql query string

Im currently new to php, i figured this is the right place to ask. When i tried to echo $empshift variable. it says in the browsers console

("SELECT SHIFT_START FROM myilogin_46.DATE_TIME_RECORDS WHERE ID = '' and r_status = 'A'").

As You can see it returns the string or the query itself, not the actual value of the query.

How do I do it so that $empshift would really return the value of the query (for example: in the database SHIFT_START column is a time datatype-> 08:30:00 in this format.).

I figured this was the reason why I can't subtract $empshift to $alterin.

foreach ($rs as $key => $value) {

    $db = new Database();
    $db->connect();

    date_default_timezone_set('Asia/Ho_Chi_Minh');
    $empshift = $db->select(array('myilogin_46.DATE_TIME_RECORDS'),'SHIFT_START',"ID = '".$id."' and r_status = 'A'");
    echo $empshift; //SELECT SHIFT_START FROM myilogin_46.DATE_TIME_RECORDS WHERE ID = '' and r_status = 'A'<br />
    $sched = $empshift;
    $alterin = date('H:i:s',strtotime($value['alterIN']));//
    echo $alterin;//08:33
    echo $sched;
    $late = $alterin-$sched;

    $db->update_imp('myilogin_46.DATE_TIME_RECORDS',array(
                'date_in'=>date('Y-m-d',strtotime($value['alterIN']))
                ,'time_in_info'=>'ALTERED IN'
                ,'time_out_info'=>'ALTERED OUT' 
                ,'time_in'=>date('H:i',strtotime($value['alterIN']))
                ,'time_out'=>date('H:i',strtotime($value['alterOUT']))
                ,'date_out'=>date('Y-m-d',strtotime($value['alterOUT']))
                ,'late'=>$late
            ),"id = '".$value['dtrID']."' and id_emp = '".$value['empID']."' and R_STATUS = 'A'");
              // echo date('H:i:s',strtotime($late));
    $db->disconnect();

this is the select () function in the Database class:

 public function select($table = array(), $column = '*', $where = null, $order = null, $limit = null){
        
        $qry = 'SELECT '.$column.' FROM '.implode(', ',$table);

        if($where != null){
            $qry .= ' WHERE '.$where;
        }
        if($order != null){
            $qry .= ' ORDER BY '.$order;
        }
        if($limit != null){
            $qry .= ' LIMIT '.$limit;
        }

    //   echo $qry.'</br></br>';
    //   exit();

        if ($this->sql($qry)) {
            return $qry;
        } else {
            echo 'dbselect sql error: '.$qry;
            exit();
        }
        
    }

Upvotes: 1

Views: 70

Answers (1)

DarkChocolate Reborn
DarkChocolate Reborn

Reputation: 43

I got it, I didnt assign the query to as a variable. instead i used for each and a getResult function.

foreach ($rs as $key => $value) {

        $db=new Database();
        $db->connect();
        
        $alterin= date('H:i:s', strtotime($value['alterIN']));
        $db->select(array('myilogin_46.DATE_TIME_RECORDS'),'SHIFT_START'
        ,"ID = '".$value['dtrID']."' and R_STATUS='A'");

        foreach ($db->getResult() as $key => $value) {
            $shiftstart = $value['SHIFT_START'];
        }
       
        $late= $alterin -  $shiftstart;
        // if($shiftstart >= $alterin){
        //    $late= "00:00:00";
           
        // }elseif ($shiftstart > $alterin) {
        //     # code...
        //     $late= $alterin - $shiftstart;
           
        // }
       

        echo $shiftstart;
        echo $alterin;
        echo $late; //
        

Upvotes: 1

Related Questions