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.

 

 

Link to comment
Share on other sites

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) . '")';

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.