tags:

views:

38

answers:

2

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
+1 for the use of array_map().
Inkspeak
mysql_real_escape_string is not needed, 'tis better to use just intval function here.
silent
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
+2  A: 

Look here:

for a safe, parameter-based approach and code sample.

Tomalak