SeanHarding Posted October 31, 2008 Share Posted October 31, 2008 I have an array with 3 id's in it... currently i'm doing the following: $my_array(2, 4, 6); foreach ($my_array as $search) { $quer = "SELECT * FROM my_table WHERE id=$search LIMIT 1"; $run = mysqli_query($connect, $quer); while ($new_array = mysqli_fetch_array($run)) { //displaying results here } } Is there a better way of doing this instead of repeating my query? Am I in the wrong forum? Quote Link to comment https://forums.phpfreaks.com/topic/130873-foreachbla-as-blb-mysqli_query/ Share on other sites More sharing options...
JasonLewis Posted October 31, 2008 Share Posted October 31, 2008 Well it depends if you want to select each ID individually or if you can combine it into one query. Sometimes you may need to perform a query in a loop. Quote Link to comment https://forums.phpfreaks.com/topic/130873-foreachbla-as-blb-mysqli_query/#findComment-679294 Share on other sites More sharing options...
SeanHarding Posted October 31, 2008 Author Share Posted October 31, 2008 I just need to display the contents of each field onto the page, relating to their id. Each value in the array will 100% be in the database. Quote Link to comment https://forums.phpfreaks.com/topic/130873-foreachbla-as-blb-mysqli_query/#findComment-679299 Share on other sites More sharing options...
SeanHarding Posted October 31, 2008 Author Share Posted October 31, 2008 I could implode the array but i'm not sure how that would work... if i: implode('AND field ', $array); Its searching the database for the three arrays in one table field... Quote Link to comment https://forums.phpfreaks.com/topic/130873-foreachbla-as-blb-mysqli_query/#findComment-679305 Share on other sites More sharing options...
PFMaBiSmAd Posted October 31, 2008 Share Posted October 31, 2008 You do need to implode the array, but do it so that you can use the mysql IN() function - http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in Quote Link to comment https://forums.phpfreaks.com/topic/130873-foreachbla-as-blb-mysqli_query/#findComment-679485 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.