I have an array of category ids and want to retrieve articles from my mysql database with these category ids. What is the best method for this?
Asked
Active
Viewed 79 times
2 Answers
8
mysql_query('SELECT * FROM articles WHERE category_id IN (\''.implode('\'',array_map('mysql_real_escape_string',$categories)).'\')');
Specify how articles are joined to categories if this is not how your db/table setup.

Wrikken
- 69,272
- 8
- 97
- 136
-
1mysql_real_escape_string is not needed, 'tis better to use just intval function here. – silent Jun 07 '10 at 15:29
-
intval would be preferred when talking about integers indeed, OP didn't mention that though (and using uuid's or even chars for categories is not that rare,) so just to be safe... – Wrikken Jun 07 '10 at 15:39
2
Look here:
for a safe, parameter-based approach and code sample.