Difference between null, empty, 0 and false by picking up the record in the database

2

I just saw an article showing how to change the null to false and 0 as well, but I'm not sure how to do that when an information is in my database and it's type 0 and 1 and I wanted it for yes or not your assignment.

 <label><h3 class="h3"><?php echo $list['Nome_da_coluna']; ?></h3>  </label> 

    
asked by anonymous 08.10.2014 / 19:47

2 answers

3

A way other than the one already mentioned is to do this directly in the bank:

$sql = 'SELECT nome, idade, IF(flag_ativo = 0, "Não", "Sim") AS esta_ativo FROM tabelaExemplo';

In this example I take the fields name, age and make an IF in the field flag_active set it to Yes or No. In php you could do it again like this:

<label><h3 class="h3"><?php echo $list['esta_ativo']; ?></h3></label> 

The gain of performance is practically irrelevant, so I also recommend the method suggested by @KaduAmaral

Link to the function IF .

For Null fields you can try the IFNULL .

    
09.10.2014 / 01:48
2

So, it's just you to make a IF , or better yet, a ternary condition .

<?php $list['Nome_da_coluna'] = ($list['Nome_da_coluna'] == '1' ? 'Sim' : 'Não'); ?>

<label><h3 class="h3"><?php echo $list['Nome_da_coluna']; ?></h3></label>

What it means, se $list['Nome_da_coluna] é igual a '1' imprima 'Sim' senão imprima 'Não' .

Always try to research and study before asking a question. We are here to help, but this type of answer you find on the net.

    
08.10.2014 / 19:54