Jump to content

query DB for records with an array of ids?


rahjiggah

Recommended Posts

Hello just wondering if someone can help me...

 

I have a select multiple form that sends the id numbers of records I want to get...

 

how do I construct the query to the mysql db to do this?

 

$array-$_POST['form-array']

 

so something like $r = ("SELECT column from 'Table' WHERE ID = (array, values, here)")

 

Thanks for any help.

 

 

If the ID column is a numeric type, the answer is rather simple:

 

$ids = array(2, 4, 8, 16);

$sql = 'SELECT columnName from TableName WHERE ID IN (' . implode(',', $ids) . ')';

 

If the column is a string type, you just have to add a couple of quotes in the right places:

 

$ids = array('I2', 'D4', 'T8', 'Q16');

$sql = 'SELECT columnName from TableName WHERE ID IN ("' . implode('","', $ids) . '")';

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.