How to run the same query for different "ids"

Asked

Viewed 31 times

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, how will the assembly of the query [using PDO]?

  • I don’t understand, you want the logic for the database or for the php code?

  • Both, rsrs. @Francisco

  • select * from table where id in (x,y,z) ,using the IN?

1 answer

2


There must be some easier way, I don’t know, but you can do it with the implode():

Using the 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 on Ideone.

Using the IN:

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

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

$conn->query($query);

See working on Ideone.

Browser other questions tagged

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