2

i'm trying to use data (user id's) from an array in a query. I want to retrieve data of the user where the id's in the array match id's in the database.

This is my array:

$ids = array("1234","1235","1250","3586");

how can i transform or use this array so i can use it to check every value if it's exists in the database. If it's exists, it has to return the users name and birthday.

2
  • 3
    are you sure that is your array? that's just an array with one element, a string. Commented Dec 18, 2014 at 12:04
  • I have fixed the quote typos of the array. Commented Dec 18, 2014 at 12:06

1 Answer 1

5

Use MySQL WHERE IN() and PHP implode()

Example:

$uidStr = implode(',', $arrUserIds); // where $arrUserIds is your array.
$sql = "SELECT * FROM Table_Name WHERE id IN ($uidStr)";

Explanation:

You already have your user ids in an array.

And you want all rows from database whose ids are contained in the array.

Use implode(), so that your array gets converted into a sting.

e.g. if you are searching with array(1, 2, 3, 4);

Above query will search for records who have either 1 or 2 or 3 or 4.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.