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();
        }
        
    }
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 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. – ADyson Jun 17 '21 at 09:46
  • I updated it, i have included the select() function – DarkChocolate Reborn Jun 18 '21 at 00: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. – ADyson Jun 18 '21 at 06: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? – Don't Panic Jun 18 '21 at 09:13

1 Answers1

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; //
        
ADyson
  • 57,178
  • 14
  • 51
  • 63