1
I made this example for you to understand how the query is being made in the bank in 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 this, I’m taking the result of the query and if it in an object, so far so good, the problem is when null values arrive, if I have a string 'NULL' or a null field the result is the same, as I can differentiate null from a string 'NULL'?
Values in the object:
'nChegada' => 'NULL',
'nExpedido' => 'NULL',
One with null value and the other with a string 'NULL'.
UPDATE
Value of n_arrival and n_state are null and of n_dispatched is a string
Code:
public function getDadoById($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',
))
The result is not the same, a text type field with NULL content returns NULL, now if the field is NULL (has no value) will be returned empty.
– Roberto de Campos
But if I take $Row["field"] and play on my object as mysql returns null, it understands in the same way as a string 'null'.
– Wictor Chaves
That point you’re missing, the
MySQL
does not returnNULL
it returns empty. What you can do is check if the column has any data.– Roberto de Campos
"does not return NULL", but it returned null
– Wictor Chaves
Check my answer that you will understand what I am talking about. Who is writing
NULL
on the screen is thePHP
and not theMySQL
. ThePHP
writNULL
when youprinta
a field without any information– Roberto de Campos
Null values in PHP
– rray