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

0

I have a table called USERS 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 the screen one (OK).

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

    
asked by anonymous 22.09.2016 / 03:15

3 answers

2

One solution would be to select all users with the 1 value in the STATUS column 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 DB if there is any user with the STATUS = 0 field, if there is 1 or more it returns erro otherwise return OK

    
22.09.2016 / 11:02
-6

Well, I think that 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 the 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";
    }

The foreach will loop as long as there is data to be manipulated. This is the best way I know to manipulate bank data, generating looping. I hope I have helped you!

    
22.09.2016 / 05:36
-8

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

    
22.09.2016 / 03:16