Reputation: 4516
I have a string of IDs. How can I get all rows for those IDs.
For example:
$string = "0,2,4";
user_ID | first_name
------------------------------------
0 james
1 mike
2 jake
3 hannah
4 john
Query should return rows 0,2,4
which are james, jake, john
.
Edit: String is sanitized, validated, and query uses prepared statement.
Upvotes: 1
Views: 423
Reputation: 309
Sorry for late..
I tried this as per RhapX its working fine.thank you so much..
SELECT * FROM tablename WHERE user_ID IN ($string);
Upvotes: 0
Reputation: 309
SELECT * FROM table WHERE id IN ("2,3,5");
Above table will give only result of 2 since it will take first id and returns only single result.
TO get all fields you need to pass with out double quotes like below.. SELECT * FROM table WHERE id IN (2,3,5);
Upvotes: 1
Reputation:
"SELECT * FROM table WHERE user_ID IN (" . $string . ")"
OR
"SELECT * FROM table WHERE user_ID IN ('" . str_replace([' ', ','], ['', "','"], $string) . "')"
Upvotes: 1
Reputation: 1683
A simple SQL query will do the trick.
SELECT * FROM tablename WHERE user_ID IN ($string);
Obviously precautions should be taken in order to validate the data string being passed in to the query.
Upvotes: 3