If you had an error check after mysql_query() it would indicate what the problem is. So, put one in there.
I happen to know that "to" is a MySQL reserved keyword and should not be used as a column name and that's probably what's causing the problem. Either change that column name to something else (which is what I recommend) to avoid further confusion, or enclose the column name inside backtick marks. Example:
$query = mysql_query("SELECT `to` FROM mail WHERE `to` = '$userid'");
I would change the column name because "to" is too vague of what it means and what data it holds.
FYI - Example code of how to retrieve data from MySQL with error checking:
http://www.phpbuilder.com/board/showpost.php?p=10648003&postcount=4
hth.
🙂