put a string in the template where you want the query result to go (like [query results]) and then do:
$result = mysql_query('SELECT * FROM table');
$output = '';
while ($row = mysql_fetch_assoc($result))
{
$output .= print_r($row);
}
$template = file_get_contents('/path/to/template');
$template = str_replace('[query results]', $output, $template);
echo $template;