How to make the return of a Query (PDO)

Asked

Viewed 630 times

1

How can I make the selected Month input bring me the selected user month and year? It is returning the user but when I input the Month to bring the user data for a given month does not come.

inserir a descrição da imagem aqui

this is the query

public function RetornaAtualizacoesFuncionarios($data,$codusuario){ 
        $WHERE = array();
        if( !empty( $codusuario   ) ) {$WHERE[] = "codusuario = :codusuario";}else{ $WHERE[] = "codusuario != '37'"; };
        if( !empty( $data   ) ) $WHERE[] = "DATE_FORMAT(data,'%Y-%m') = :data";         
    try {
         $Query = "SELECT 
        DISTINCT(usuario),
                date_format(data,'%d/%m/%Y %H:%i:%s') as data,
                codigo,
                nome 
                    FROM funcionarios
                         WHERE codusuario = '$codusuario'
                             ORDER BY data DESC  ";
                             if( !empty($WHERE) ) $this->$Query .= ' WHERE '.implode(' AND ', $WHERE );

//echo "<pre>"; print_r($_POST); exit;
        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();
            }
        }

public function RetornaUsuarios(){
    try {
        $Query = "SELECT distinct(funcionarios.codusuario),funcionarios.usuario 
        FROM funcionarios 
            LEFT JOIN usuarios ON funcionarios.usuario=usuarios.nome
            WHERE funcionarios.codusuario != '37'
                  ";
        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();
            }
        }


    }

and that’s the input

<form class="form"  id="exibefuncionarios" method="post" target='_blank' action="gerar">

        <fieldset>
            <div style="border-radius:5px;padding:10px;">
                <table class="custom-select" cellspacing='0' width='100%' border ='0'>
                    <tr>
                        <td class='label_1'>Período:</td> 
                        <td>
                            <input style="width: 290px;" name="data" type='month'/>
                        </td>
                    </tr>
                </table>
            </div>
        </fieldset>

        <fieldset>
            <div style="border-radius:5px;padding:10px;">
                <table class="custom-select" cellspacing='0' width='100%' border ='0'>
                    <tr>
                        <td class='label_1'>Funcionário:</td> 
                        <td>
                            <select style="width: 290px;" name="codusuario" id="codusuario">
                                    <option value=''>--Selecione--</option>
                                        <?php foreach($lista_funcionarios as $funcionario){ ?>
                                    <option value='<?php echo $funcionario["codusuario"]; ?>'><?php echo $funcionario["usuario"]; ?></option>
                                <?php } ?>
                              </select>
                        </td>
                    </tr>
                </table>
            </div>
        </fieldset>

             <br/>
        <button id="gera_rel_funcionario" class="BlueButton" type="submit">Gera Relatório</button>
        </form>
        </div>  
    </div>

1 answer

0


cannot have two Where in sql and also cannot after order by

I will do mode or query by employee or by code, I recommend to you separate a method to filter by date and another by user

public function RetornaAtualizacoesFuncionarios($data,$codusuario){ 

    try {

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

        if(!empty($data)){

            $Query .= "WHERE data = $data";

        }else {

            if(!empty($codusuario) && empty($data)){
                $Query .=  WHERE codusuario = '$codusuario';
            }

        }   
        $Query .=  "ORDER BY data DESC  ";


        //echo "<pre>"; print_r($_POST); exit;
        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();
        }
    }

If returning a single result you don’t need while and only

$_result = $p_sql->fetch(PDO::FETCH_ASSOC after return result;

  • I will try to do so. And thank you.

  • error in the code

  • which error? can give more details.

  • nothing appears on the screen , well the way I did it lists the name only the input Month is not entering the query .

  • edita, after the old code puts like this now

Browser other questions tagged

You are not signed in. Login or sign up in order to post.