title: Is there an easier way to do a PHP foreach loop involving mySQL?
body: <p>I have a script that has a list of country ID codes as $item_details[countries] which are coma delimited.</p>
  
  <p>In order to display the names of the countries (stored in mySQL), it needs to currently run each loop to get the country name and then display it.</p>
  
  <p>To prevent the script from connecting to the mySQL for each and every single name, could there be an easier way of getting all the names at once and then displaying them in a loop afterwards, so that there is only 1 mySQL query?</p>
  
  <p>Such as:</p>
  
  <pre><code>$all_country_names = $db-&gt;get_sql_field("SELECT name,id FROM db_countries");
  </code></pre>
  
  <p>Here is what I have now:</p>
  
  <pre><code>// $item_details['countries'] are the coma delimited country IDs
  
  // Changing the coma delimited IDs into an array
    $array = explode(',', $item_details['countries']);
  
  // Running the loop
    foreach($array as $key=&gt;$value){
  
  // Getting each country name from the database based on the ID
    $country_name = $db-&gt;get_sql_field("SELECT name FROM db_countries WHERE id='".$value."'");    
  
  // Displaying each country name
    echo $country_name;
    echo ", ";
  }
  </code></pre>
  
  <p>Thank you in advance for any help :)</p>