Reputation: 155
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.
Upvotes: 2
Views: 50
Reputation: 23948
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.
Upvotes: 5