Jump to content

[SOLVED] Using an array to SELECT * FROM table using mySQL -- how to do it?


Bobafart

Recommended Posts

I have created an array which consists of IDs (integers) of news articles.  My array is called  $different_articles().  Here is a data sample of its elements:  Array("3",  "7", "11", "32", "443", ... "4456", "4532" etc.. ) it's BIG!

I should add that the array has a new set of values everytime the script is run... so the elements are never the same.

I now want to use the IDs from this array to SELECT the articles with these IDs out of my mySQL table.. how do I do it? 

I have never used an array to select items from a table so I am lost.. Google didn't help either.

[code]
$q = "SELECT * FROM table WHERE id = 'what goes in here?'";
[/code]

thanks
well I am not sure why you would want to select so many articles in the first place - do you have categories or something that you can select instead of individual IDs

if you need to do IDs, do something like this...
[code]
$tmp='';
foreach($different_articles() AS $id){
    array_push($tmp,'id=' . $id);
}
$where= '(' . implode(' OR ',$tmp) . ')';

$q = "SELECT * FROM table WHERE" . $where;
[/code]
thanks drifter.. while you were posting I found another solution (I tested it, it works)

[code]

$in = implode(', ', $different_article);
$queryRelatedHeadlines = "SELECT id, headline, intro, body, author, date, source, vote, xmlsitetype from anews1 where id in ($in)'";
 

[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.