Multiple SQL queries or cache array - which is quicker?

Just wondering about something I often have to do and which way would be better and quicker.

Suppose table 1 has names & id numbers (id-username: 1-“Bob”, 2-“Simon”, 3-“insaniak” etc…) and table 2 has messages or uploads of anything really (id-userid-message: 1-3-“hi from insaniak”,2-3-“hello again from insaniak”) etc…

Now, I am running through the table and printing out all the messages, for example. I want to retreive the username for each user from table 1 associated with each userid. Is it quicker to:

  1. initially run a query to gather this whole table into a big associative array?
    or
  2. query for individual entries each time with “SELECT username FROM table1 WHERE id=‘2’” ?

What if there were 100 usernames, 1000? 10,000? Anybody have any thoughts?