 <p>Why not just grab all the right information in one query from the start along the lines of something like this:</p> <pre><code>select a.exID, examples.someField, examples.someOtherField from examples join ( SELECT exID FROM examples WHERE concept='$concept' ORDER BY RAND() limit 5 ) a on a.exID=examples.exID </code></pre> <p>Then you just just pop them into an array (or better yet object) that has all the pertinent information in one row each time.</p>