title: Use PHP Loop to Query MYSQL Database Multiple Times
body: <p><strong>Context:</strong> I'm querying a database to return a specified number of monsters to be used in a fight within a simple web based RPG.</p>
  
  <p><strong>Problem:</strong> I have a straightforward MYSQL query that includes a WHERE clause to determine the "level" of the monsters. In a previous function I'm building an array that includes the levels of the corresponding monsters that should be returned; something like:</p>
  
  <pre><code>$levels = Array( [0] =&gt; 3 [1] =&gt; 1 [2] =&gt; 1 ) 
  </code></pre>
  
  <p>So in the above array, the Value corresponds to the level. So I'm looking to return 3 monsters -- Level 3, Level 1 and Level 1.</p>
  
  <p>Simple enough. Now I want to query MYSQL with the following and presumably use a loop to do the query 3 times to get my 3 monsters; something like:</p>
  
  <pre><code>$query = "SELECT *, CardHP as EncounterCardHP, CardAP as EncounterCardAP";
  $query .= " FROM Cards";
  $query .= " WHERE CardTypeId = 1 AND CardRarity = {$level}";
  $query .= " ORDER BY RAND() LIMIT 1";
  </code></pre>
  
  <p>So in the above query, the variable {$level} is the Monster Level from the previous array. The loop formatting is where I'm struggling. I'm trying something like:</p>
  
  <pre><code>while ($i = $levels) {
       $query = "SELECT *, CardHP as EncounterCardHP, CardAP as EncounterCardAP";
       $query .= " FROM Cards";
       $query .= " WHERE CardTypeId = 1 AND CardRarity = {$i}";
       $query .= " ORDER BY RAND() LIMIT 1";
   $result_set = mysql_query($query, $connection);
   confirm_query($result_set);
   return $result_set;    
  }
  </code></pre>
  
  <p>I think this is close, but I'm not seeing how I can get this to ultimately give me a single array of monsters from MYSQL. It seems in the above, I'm building 3 separate arrays? Do I need to use array_merge(), or is there a simpler more elegant way to do this? If I were getting all the same level monsters I could simply just run the query 1 time and LIMIT 3, but I need monsters to correspond to what the previous array shows.</p>
  
  <p>Making sense? Help is always appreciated.</p>