Fetching data from the database

There is an array. It is required to select from the database all rows where the value of the specified column will be equal to one of the array elements.

The crux of the matter is how to do it correctly, short and fast. Everyone knows how to invent, but a competent approach is required.


$arr = array(0=>1, 1=>2, 2=>4 /* ... n=>m */)
$sql=mysql_query("SELECT title,value FROM $someTable WHERE id='    $arr'");
while ($data=mysql_fetch_assoc($sql))
//do something

Answer 1, authority 100%

Usually they do this (I will write it in separate lines to make it clearer):

$sql = mysql_query(
    "SELECT title, value FROM $someTable WHERE id IN (" . 
    join(',', $array) . 

It works quite well until the length of the request hits max_allowed_packet.

And yes – it’s almostcorrect, short and quick 🙂
To be completely correct, array elements (depending on the type) should be quoted / escaped. In php it seems to be the function mysql_escape_string()