Select Mysql with an array in a table

1

I have a table in the bank called actuation, in which generated the ID's of the areas chosen by the user ex: 1,5,4,9,15

I need the result of the query in Mysql to return all the users that have been selected in a checkbox list

Can someone give me a glimpse of how to do this search, even without repeating the user if it fits into more than one chosen area of action?

I'll simplify with the IN Query, the result was the same

Using

SELECT * FROM usuarios WHERE atuacao_usuario IN (1,5,6)

Show me the results of users in the table already with these areas selected as in the image  

Now if I remove the numeral 1 query SELECT * FROM usuarios WHERE atuacao_usuario IN (5,6) does not return any result

    
asked by anonymous 29.04.2018 / 17:16

1 answer

1

You can use OR within SELECT . So:

SELECT * FROM usuarios WHERE atuacao_usuario = 1 OR atuacao_usuario = 2 ...

To do this automatically. Your code will look like this:

$array_atuacao = array(1,5,8,7);

$sql_func = "SELECT * FROM usuarios WHERE ";

foreach($array_atuacao as $atuacao){

    $sql_func .= "atuacao_usuario = ".$atuacao." OR ";

}

$sql_func .= substr($sql_func, 0, -3);


$result_func = $mysqli->query($sql_func);   

    while($func = $result_func->fetch_assoc()) {               

        echo $func['nome_usuario'] .' - '.$func['atuacao_usuario'];        
    };

Or, as quoted by @LeoCaracciolo, you can use IN

SELECT * FROM usuarios WHERE atuacao_usuario IN (1,2,3,4)

EDIT

Try to do this:

$array_atuacao = array(1,5,8,7);

$sql_func = "SELECT * FROM usuarios WHERE ";

foreach($array_atuacao as $atuacao){

    $sql_func .= "FIND_IN_SET('$atuacao', atuacao_usuario ) OR ";

}

$sql_func .= substr($sql_func, 0, -3);
    
29.04.2018 / 19:08