SQL query returning null value

2

SQL Fiddle DB Simulation: link

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

autosalvar.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 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}

Edition: autosalvar.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);
    
asked by anonymous 24.01.2017 / 19:11

2 answers

1

One way to do this would be:

$sth = $conecta->prepare('SELECT * FROM nfe WHERE id = ?');
$sth->execute(array($nNFe));
$rs = $sth->fetchAll();
print_r($rs);
    
24.01.2017 / 20:30
0

Have you checked the result you're getting on the line below?

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

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

var_dump($resultado);

Another doubt. When you run the following SQL statement below, replacing the parameters with actual values, do 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 loading correctly.

    
24.01.2017 / 19:23