How to use AND?

1

I would like to know where I can put AND tipregistro = 'mysql' so that when both !empyt and when you do not have tipregistro = 'mysql'

this is my query

public function RetornaAtualizacoesFuncionarios($data,$codusuario){ 
        $WHERE = array() ;
       if( !empty( $codusuario    ) ) {$WHERE[] = "codusuario = $codusuario";};
       if( !empty( $data   ) ) {$WHERE[] = "DATE_FORMAT(data,'%Y-%m') = '$data' AND tipregistro = 'mysql' ";};

    try {

         $Query = "SELECT 
                        DISTINCT(funcionarios.usuario),
                        date_format(funcionarios.data,'%d/%m/%Y %H:%i:%s') as data ,
                        codigo,
                        nome 
                            FROM funcionarios
                                      "; 

        if( !empty($WHERE) )$Query .= ' WHERE '.implode(' AND  ', $WHERE );

        // echo "<pre>"; print_r($Query);

        include_once $_SESSION['pmodel'].'/mysqlconnection_class.php';
               $p_sql = MysqlConnection::getInstance()->prepare($Query);

                $_retorno = array(); 
                if($p_sql->execute()) {
                    while ($_result = $p_sql->fetch(PDO::FETCH_ASSOC))  {
                        $_retorno[] = $_result; 
                    }
                }
                return $_retorno;
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
    }
    
asked by anonymous 05.10.2016 / 17:56

1 answer

1

Simply insert the condition into the array $WHERE out of if (!empty($data)) :

public function RetornaAtualizacoesFuncionarios($data,$codusuario){ 
    $WHERE = array() ;
    if( !empty( $codusuario    ) ) {$WHERE[] = "codusuario = $codusuario";};
    if( !empty( $data   ) ) {$WHERE[] = "DATE_FORMAT(data,'%Y-%m') = '$data'";};
    $WHERE[] = "tipregistro = 'mysql'";

    try {

         $Query = "SELECT 
                        DISTINCT(funcionarios.usuario),
                        date_format(funcionarios.data,'%d/%m/%Y %H:%i:%s') as data ,
                        codigo,
                        nome 
                            FROM funcionarios
                                      "; 

        if( !empty($WHERE) )$Query .= ' WHERE '.implode(' AND  ', $WHERE );

        // echo "<pre>"; print_r($Query);

        include_once $_SESSION['pmodel'].'/mysqlconnection_class.php';
               $p_sql = MysqlConnection::getInstance()->prepare($Query);

                $_retorno = array(); 
                if($p_sql->execute()) {
                    while ($_result = $p_sql->fetch(PDO::FETCH_ASSOC))  {
                        $_retorno[] = $_result; 
                    }
                }
                return $_retorno;
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
    }
    
05.10.2016 / 18:13