Bobafart Posted December 16, 2006 Share Posted December 16, 2006 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 Link to comment https://forums.phpfreaks.com/topic/30906-solved-using-an-array-to-select-from-table-using-mysql-how-to-do-it/ Share on other sites More sharing options...
drifter Posted December 16, 2006 Share Posted December 16, 2006 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 IDsif 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] Link to comment https://forums.phpfreaks.com/topic/30906-solved-using-an-array-to-select-from-table-using-mysql-how-to-do-it/#findComment-142567 Share on other sites More sharing options...
Bobafart Posted December 16, 2006 Author Share Posted December 16, 2006 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] Link to comment https://forums.phpfreaks.com/topic/30906-solved-using-an-array-to-select-from-table-using-mysql-how-to-do-it/#findComment-142572 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.