How can I differentiate a 'NULL' string from a null fact value?

1

I made this example for you to understand how the query is done in the bank on my system.

$sql = "SELECT * FROM tabela";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo $row["campo"];
    }
}

My problem is the following, I'm getting the result of the query and if it is in an object, until then, the problem is when null values arrive, if I have a 'NULL' string or a null field the result is the same, how can I differentiate null from a 'NULL' string?

Values in the object:

   'nChegada' => 'NULL',
   'nExpedido' => 'NULL',

One is null and the other is a string 'NULL'.

UPDATE

Value of n_chegada and n_estate are null and of n_query is a string

Code:

publicfunctiongetDadoById($id){$stmt=$this->conexao->getQuery("select * from " . $this->getTabela() . " WHERE 'id' = " . $id);
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $cla = null;
    if (count($results) > 0) {
        $atributos = $this->getAtributos();
        $classe = 'model\dominio\' . ucfirst($this->getTabela());
        $cla = new $classe();
        foreach ($atributos as $atributo) {
            $metodo = 'set' . ucfirst($atributo);
            if(is_null($results[0][$this->atributoToDb($atributo)])){
                $cla->$metodo(NULL);
            }else{
                $cla->$metodo($results[0][$this->atributoToDb($atributo)]);
            }
        }
    }
    return $cla;
}

I tried with the example of Roberto de Campos, but the result was the same:

model\dominio\Funcionario::__set_state(array(
   'id' => '1',
   'pis' => '12345678901',
   'numeroCarteira' => '12345',
   'serie' => '1234',
   'nome' => 'Trabalhador Padrão',
   'mae' => 'Mãe do Trabalhador Padrão',
   'pai' => 'Pai do Trabalhador Padrão',
   'dataNascimento' => '10/10/1988',
   'sexo' => '1',
   'estadoCivil' => '1',
   'naturalidade' => '2680',
   'rg' => '1234567890',
   'cpf' => '12345678901',
   'cnh' => '12345678901',
   'tituloEleitoral' => '123456789012',
   'secao' => '1234',
   'zona' => '123',
   'localEmissao' => '2680',
   'nacionalidade' => '7',
   'nChegada' => 'NULL',
   'nExpedido' => 'NULL',
   'nEstado' => 'NULL',
   'observacao' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.',
   'email' => '[email protected]',
   'senha' => '123',
   'dataEmissao' => '2017-10-23 13:12:42',
))
    
asked by anonymous 23.10.2017 / 14:57

2 answers

5
The MySQL does not return a string with content NULL when the field is null, it returns a column with no data, so you can check whether the return has given or not with the function is_null :

$sql = "SELECT * FROM tabela";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if (is_null($row["campo"]))
            echo "Este campo não tem dado nenhum";
        else
            echo "Este campo tem o seguinte conteúdo: ".$row["campo"];
    }
}

Another way for you to check this NULL return is by using === NULL :

$sql = "SELECT * FROM tabela";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if ($row["campo"] === null)
            echo "Este campo não tem dado nenhum";
        else
            echo "Este campo tem o seguinte conteúdo: ".$row["campo"];
    }
}
    
23.10.2017 / 15:08
1

You can filter the nulls in mysql's own selection, like this:

SELECT ProductID, Name, Color
FROM AdventureWorks2008R2.Production.Product
WHERE Color IS NULL

And if the color were a text value it would look like this:

SELECT ProductID, Name, Color
FROM AdventureWorks2008R2.Production.Product
WHERE Color = 'NULL'

These fields are treated differently by the bank.

    
23.10.2017 / 15:02