Select Mysql with an array in a table

Asked

Viewed 534 times

1

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

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

someone can give me a light on how to perform this search, even without repeating the user if it fits in more than one chosen area of action?

I will simplify with Consultation IN, that the result was the same

Using

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

Show me the results postpussuo users in the table already with these areas selected as in the image inserir a descrição da imagem aqui

Now remove the number 1 from the query SELECT * FROM usuarios WHERE atuacao_usuario IN (5,6) does not return any results

  • Enter part of the code you’ve already made in php.

  • Good afternoon @Andreicoelho, I don’t have much concrete yet, I just created a common query,

  • Oops... better now I can answer

  • WHERE atuacao_usuario IN (1,5,8,7); https://www.w3resource.com/mysql/comparision-functions-and-operators/in-function.php

  • There’s an Edit I put in the answer. Try it later.

1 answer

1


You can use the OR within the SELECT. Thus:

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 so:

$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);
  • 1

    You can use the IN clause to replace many OR conditions

  • Valew @Andreicoelho, it worked here, I only altered The concatenation that came there in the consultation, but it worked right here, thanks!!

  • @Marcospaulo does not forget to approve! Hug!

  • @Leocaracciolo yes. I will put this option.

  • Putting into production, did not work, only works if the first id ( Performance ID ) is marked. Where can I put a functional example? Ai vc will have a better idea

  • "only works if the first id ( Performance ID ) is marked" as well?

  • Better you insert everything into question... HTML, PHP, etc..

  • Okay, I just edited the question

  • @Andreicoelho, I put an online example http://consultoriatelejet.com.br/teste.php?id=5,1 Changes the id`s according to the search

Show 5 more comments

Browser other questions tagged

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