How to select users with the STATUS field =1 in the user table?

Asked

Viewed 156 times

0

I have a table called USUARIOS with the fields NAME, STATUS. Suppose I have 10 registered users.

How can I check if the 10 users have STATUS = 1 and if so, displays on screen one (OK).

I know you have to make a SELECT, so far quiet, but what is the best way to do it without using array or loop?

3 answers

2

A solution would be to select all users with the value 1 in the column STATUS and count them.

$con=mysqli_connect("localhost","utilizador","password","base_dados");
if (mysqli_connect_errno())
{
    echo "Erro na coneção á BDL: " . mysqli_connect_error();
}

// Executa a query
mysqli_query($con,"SELECT status FROM usuarios where status = 1;");

$n_utilizadores = mysqli_affected_rows($con);

echo "Existem " . $n_utilizadores. " com STATUS = 1";

if ($n_utilizadores == 10)
{
    echo "OK";
}

You can also invert the query and ask the BD if there is a user with the field STATUS = 0, if there is 1 or more returns erro otherwise returns OK

-6

Good guy think to do this the best way is to use a loop with foreach, where it will bring you all the information searched in the database with select allowing you to manipulate the data in the best way you like.

//Example of the variable that stores your search with select

  $armezanaValoresDoSelect= "sua busca no banco";

    foreach($armazenaValoresDoSelect as $NewValues){
         //faça uma validação onde se a $NewValues['status'] == 1 echo "ok";
    }

Foreach will loop as long as data is manipulated. This is the best way I know how to manipulate database data, generating loopings. I hope I helped her!

-9

foreach is a good option, no loop will not be possible unless you are using Bootstrap, then maybe there are other options.

  • I want to do something like this: If you display 10 records returned by SELECT, check if 10 have 1 in the STATUS field, is there any example ?

  • Guy if you don’t use bootstrap will have to be by loop. I’ll make an example in for. Puts everything in an array and does it, impossible otherwise.

  • I thought here, as you have various information for each user, each user has to be an array within a mother array, it will get pretty confusing friend.

  • @Matheuscardozo the answer field is for a valid answer, for tips can use the comment field, you can still edit your answer and put an example... Avoid negatives... Gl ;)

  • @Matheuscardozo What does bootstrap have to do with his problem? Give it a review ai man..

Browser other questions tagged

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