Mysql SUM function and return data

Asked

Viewed 305 times

0

The function below does not return me the expected.

I want her to return a sum and the fields with the id colaboador , but in Javascript I get an error, and I believe it is in PHP.

select works perfectly running direct on phpMyAdmin.

public function select($id) {

       $return_values = array();
    try {
        $sql = "SELECT SUM(custo_preco) as SOMA , custo_descricao,custo_id,custo_data,custo_mes,custo_local,custo_preco FROM tb_custo WHERE colaborador_colaborador_id= :colaborador_colaborador_id;";
        $p_sql = Conexao::getInstance()->prepare($sql);
        $p_sql->bindValue(':colaborador_colaborador_id', $id);
        $execute = $p_sql->execute();
        if ($execute) {
            while ($array = $p_sql->fetch(PDO::FETCH_OBJ)) {
                $return_values['linhas'].="<tr data-id='{$array->custo_id}'>"
                        . "<td class='actions'> "
                        . "<a href='#' name='botao_editar_custo' class='btn btn-primary btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Edit'><i class='fa fa-pencil'></i></a>"
                        . "<a href='#' name='botao_excluir_custo' class='btn btn-danger btn-xs' data-toggle='modal' data-target='#delete-modal' data-toggle='tooltip' data-placement='top' data-original-title='Delete'><i class='fa fa-times'></i></a>"
                        . "<td>{$array->custo_descricao}"
                        . "<td>" . date('d/m/Y H:i', strtotime($array->custo_data)
                                . "<td>{$array->custo_local}"
                                . "<td>{$array->custo_preco}");

            }
             $return_values['soma'] = $array->SOMA;
        } else {
            $return_values['erro'] = "Erro ao recuperar dados.";
        }

        return $return_values;
    } catch (PDOException $e) {
        return "Erro, contate o suporte!\n" . $e->getCode() . " Mensagem: " . $e->getMessage();
    }
}

Javascript function

function preencher_table_custo() {
$.ajax({
    url: 'formularios/custo/php/select.php',
    type: 'POST',
    dataType: 'json',
    data: {id: $("input[name='colaborador_logado']").val()},
    beforeSend: function () {
        $(".formulario_geral_custo table tbody").html("<tr data-id=''><td colspan='9'><img src='./imagens/loading.gif' height='26' width='26' align='center'>");
    },
    success: function (data) {
        alert(data.soma);
        $(".formulario_geral_custo table tbody").html(data.linhas);
        $(".formulario_geral_custo table tbody > tr").hover(function () {
            $(this).toggleClass("hover");
        });

    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert(errorThrown);
        $(".mensage").css("background", "#980000");
        $(".mensage").text("Verifique sua conexão com a internet e tente novamente.");
        fadeGeral(4000);
    }
});
}
  • Already tried without the semicolon at the end of SELECT?

  • Yes, it’s still not working

  • Have you called the url (Formularios/custo/php/select.php), directly in a browser window? The return is correct in this window?

  • That’s right, I do an ajax request and call the url , and the url calls the function and passes the parameters to the one on the DAO

  • You are returning an HTML code through PHP, but in Javascript you are stating that it is JSON (dataType: 'json').... and this will give error.

  • Yes, but in case I am returning an array, I would have to use JSON, or not?

  • Array is different from JSON. Test if your JSON is correct at: http://jsonlint.com/

  • Beauty. I am now in college, but tomorrow I will try. But in the case how I will return an array with several positions and access it in javascript?

  • Returning data in JSON and creating HTML in JAVASCRIPT from JSON data.

  • I see in the code return $return_values; Where does it go? Who calls this function does echo? what error appears in Javascript?

  • I forgot to post, I have a php file that gets and returns the values, which I call in the java script URL. `<? Php include_once ( 'dao.php'); $ Dao = new DaoCusto (); $ Resultado = $ dao-> selecione ($ _ POST [ "id"]); json_encode echo ($ resultado); `

Show 6 more comments
No answers

Browser other questions tagged

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