Jump to content

Recommended Posts

Hey guys,

 

As the title says I'm trying to get table data based on an array I have.

 

Example:

 

I have an array (array(1,2,3,4)), lets say that those values are IDs in a table. How would I go about getting the names from a table those values(IDs) are associated with?

 

 

Thanks! 

implode the array into a list, with a comma as the separator character, then use that comma separated list in an IN() comparison in a WHERE clause in your query.

 

ref:

http://php.net/implode

http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in

 

are these values coming from external data and would need to be properly validated, cast, or put into a prepared query to prevent sql injection?

Edited by mac_gyver

Awesome, that helped out immensely, thank you!

 

How would I go about selecting the names individually based on the array number in the same situation?

 

Example:

 

Array(1,2,3,4) correspond with (Bob, Joe, Tom, Bill) in the table. How would I go about just selecting 2 and 4 in the array? In this example there could be 100+ id's in the array, but somewhere down the line I may just need the info from Bob and Tom

Well array(1,2,3,4) is really array(0=>1,1=>2,2=>3,3=>4). And the names are array(0=>Bob,1=>Joe,2=>Tom,3=>Bill). There's multiple ways you can do this. You can use array_search to find the key based on id. But this is just based on the information you have given. There's probably easier ways to do what you are trying to do if we knew what you were actually doing and how you were doing it.

Edited by akphidelt2007
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.