How to execute the same query for different "ids"

0

I have an array with the following IDs:

Array ( [0] => 2 ) Array ( [0] => 3 ) Array ( [0] => 5 )

The query should return all the values found for these IDs, so what will be the assembly of query [using PDO]?

    
asked by anonymous 25.08.2017 / 01:20

1 answer

2

It should have some easier way, I do not know, but you can do this with implode() :

Using OR :

$ids = implode(" OR ID=", $array); //Concatena os arrays com uma string no meio de cada

$query = "SELECT * FROM tabela WHERE ID=$ids"; //Query a ser executada

$conn->query($query); //Executando a query ($conn é a variável de conexão)

See working at Ideone .

Using IN :

$ids = implode(",", $array);

$query = "SELECT * FROM tabela WHERE ID IN ($ids)";

$conn->query($query);

See working at Ideone .

    
25.08.2017 / 01:36