The Flash end of this problem can be handled using LoadVars() which is an object set up to communicate with the serverside script.
This script can be written in a variety of languages (PHP, ASP and ColdFusion are probably the most common).
This script contains the query that is run on the database, retrieves the result and sends it back to Flash.
Actually, since this is such a common question I'm going to put together an FAQ for it...