1

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();
        }
        
    }
4
  • Presumably your $db->select function returns the generated SQL rather than the query results. But since we can't see the code for that function, we can't tell you why that is, or what to do next. Commented Jun 17, 2021 at 9:46
  • I updated it, i have included the select() function Commented Jun 18, 2021 at 0:11
  • Thanks. Again we don't know what $this->sql does, but it appears that if the query succeeds the select function returns a copy of the generated SQL. I have to say that doesn't seem very useful (it would be more useful to return that as a debugging tool if there is an error), it seems like a design flaw - unless the class provides you with some other way to get access to the results of a SELECT query. Commented Jun 18, 2021 at 6:55
  • 1
    Hard to say without seeing sql(), but maybe the select() method is only supposed to generate the SQL, which should then be actually executed against the DB? Commented Jun 18, 2021 at 9:13

1 Answer 1

1

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; //
        
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.