First I feel compelled to say that you should be using PDO, especially since PDOStatement::fetchAll does most of what you want. This tutorial looks relatively decent.
Here's a bit of a re-write, but it will do everything you want it to:
// let yourself cache the connection. That will save you processing time
function getusers( $connection, $dbname = DB_NAME )
{
if( !$connection )
{
return FALSE; // we can't do anything here.
}
// Selecting a db inside of a function can cause problems for others using
// your code (if you use $connection as a parameter). Instead explicitly
// use the database
$query = "SELECT user_name FROM `$dbname`.`userinfo`";
// you may want to make this cause a return of something other than FALSE,
// but you definitely want a way to have some form of early escape here.
$result = mysql_query($query, $connection) or return FALSE;
$final = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
// append the current user_name to the $final array
$final[] = $row['user_name'];
}
// you'll need to close the connection elsewhere.
return $final;
}
In the second file:
include('usernames.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$users = getusers( $conn, $dbname ) or die( "MySQL Error: " . mysql_error() );
// foreach... as... will loop through your array automatically.
foreach( $users as $user )
{
// $user refers to each user_name fetched in getusers
getTax( $user, $connection, $dbname );
}
// and here is where the cost saving of caching your connection is found:
function getTax( $user, $connection, $dbname = DB_NAME )
{
if( !$connection )
{
return FALSE; // we can't do anything here.
}
$result = mysql_query( "SELECT * FROM TAX_TABLE WHERE USER_NAME = '$user'",
$connection ) or return FALSE;
// continue as normal
}