SQL query returning null value

Asked

Viewed 107 times

2

BD simulation in SQL Fiddle: http://sqlfiddle.com/#! 9/3f62be/1

Javascript:

// Auto-salvar
    $(function () {
        $.post("actions/autosalvar.php", function (data) {
            console.log(data);
            $("#cliente").val(data.cliente);
        }, "json");

        setInterval(function () {           
            var dados = $('#meu_form').serialize();
            $http({
              method  : 'POST',
              url     : 'actions/autosalvar.php',
              dataType: 'json',
              data : {dados: dados },
              headers : {'Content-Type': 'application/x-www-form-urlencoded'}
             })
        }, 2000);
    }); // Fim Auto-salvar

autosave.php

// Pegar os dados postados no formulário
    $dados = json_decode(file_get_contents('php://input'), true);
    $p = $dados['dados']; // pega o serializado do AJAX
    parse_str($p, $dados['dados']); // transforma em array de PHP

Form:

<form id="meu_form">
    <input type="text" name="cliente" id="cliente" style="margin-left: 10px; width:820px;"/>
</form>

When performing a foreach in $resultado:

foreach($resultado as $res){
    $cliente = $res['cliente'];
    echo $cliente;
    if($cliente == NULL){echo "Campo cliente está NULL!\n";}
    //echo json_encode(array('cliente' => $cliente));
}  

The return [PROBLEM]:

Campo cliente está NULL!
{"cliente":null,"transportadora":null}

Editing: autosave.php

$query = $conecta->prepare("SELECT * FROM nfe WHERE id=:id_nfe");
$query->bindValue(':id_nfe',$nNFe,PDO::PARAM_STR);
$query->execute();
$resultado = $query->fetchAll(PDO::FETCH_ASSOC);

2 answers

1

A way would be like this:

$sth = $conecta->prepare('SELECT * FROM nfe WHERE id = ?');
$sth->execute(array($nNFe));
$rs = $sth->fetchAll();
print_r($rs);
  • You have been mentioned here: http://meta.pt.stackoverflow.com/q/5719/132 - I invite you to express yourself.

  • @Victorstafusa, I manifested myself there through a comment. Thank you for the warning.

0

You have already checked the result you are getting in the line quoted below?

$resultado = $query->fetchAll(PDO::FETCH_ASSOC);

Use a var_dump on the cited variable to check the values and see if the result you really want is in $result[n]['client'];

var_dump($resultado);

Another question. When you execute the following SQL sentence below, replacing the parameters with real values, you get the expected results?

"SELECT * FROM nfe WHERE id=:id_nfe AND id_emitente=:id_emt AND situacao='edicao' AND (retorno_sefaz != 'autorizada' OR retorno_sefaz IS NULL)"

Do the following checks to make sure the values are being loaded correctly.

  • Igor, I’ve done it print_r in the variable $resultado, the field really goes blank: [cliente] =>

  • When I replace the values of ID and nNFe by constants, still is returned null.

  • And running the direct query in the database with the respective values being constant, is I try to return? Also check what you are receiving via POST, because it is the METHOD you are using. Use a print_r($_POST); or var_dump($_POST);

  • Igor, accept chat: http://chat.stackexchange.com/rooms/52382/consulta-sql-returning-value-null

Browser other questions tagged

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