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

Asked

Viewed 93 times

2

I just saw an article showing how to change the null to false and 0 as well, but I am not knowing how to do this when an information is in my database and it is of type 0 and 1 and wanted to yes or no in its assignment.

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

is returning select working normal but I can’t change it to yes or no in PHP it only shows me 0 and 1, I need a way to run because I’m not able to implement.

  • Welcome to Stack Overflow. First of all, this is a multi-person community of questions and answers. I recommend you start by doing a [tour] to better understand the goals of the community ;)

  • I tried to improve the question but I still don’t understand what you want. It would be nice to [Dit] the question and make it clearer. Try to be careful in the text so that people can understand what you want and can help you.

2 answers

3


A different way to do this directly at 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 do an IF in the field flag_active setting it as Yes or No. In php you could do it again like this:

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

The performance gain is virtually irrelevant, so I also recommend the method suggested by @Kaduamaral

Link to the function IF.

For Null fields you can try the IFNULL.

  • As the database knows which field the if is referring to?

  • You should highlight the field in IF. In my example I put few significant names. I will edit and put clearer names;

  • It is also worth using both, in cases necessary: IF(IFNULL(flag_ativo,0) = 0, "Não", "Sim")

  • Kaminary thanks for taking my case, I will be testing this method I am very grateful.

  • Okay @Angeloaugusto, just be careful, because doing this in the bank the network traffic will increase, since you are transferring 3 characters instead of 1 number for each record. If the database is localhost the difference is negligible.

  • Thanks for the warning I’ll take care yes Thank you very much =]

Show 1 more comment

2

So, you just make one 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>

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

Always search and study before asking a question. We’re here to help, but this kind of answer you find all over the Internet.

  • Thank you very much for looking at the emu if my dear but I have already done exactly so in fact was the first technique I used and did not succeed. Even so I am very grateful my dear =]

Browser other questions tagged

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