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?
+8
A:
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
2010-06-07 15:23:37
+1 for the use of array_map().
Inkspeak
2010-06-07 15:26:22
mysql_real_escape_string is not needed, 'tis better to use just intval function here.
silent
2010-06-07 15:29:07
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
2010-06-07 15:39:08