Select multiple rows from the MySQL table

advertisements

I have a session that contains an array and that array is filled with id's. What is the best way to select all the rows from a MySQL table that correspond to these id's?

So I need something like:

SELECT * FROM table WHERE id = $_SESSION['ids']

Obviously, this doesn't work, since $_SESSION['ids'] is an array.


You can just use IN SQL operator. In this case your query will look like

$sql = 'SELECT * FROM table WHERE id IN ("' . implode('","', $_SESSION['ids'] . '")';

This code will produse a query like following:

SELECT * FROM table WHERE id IN ("1", "2", "foo");

Hope it helps