Hey guys

I have an SQL table with users' messages and here's what the values are:

id, uid, to, sent, msg, date

id, the unique ID with auto increment (and primary key)
uid, the ID of the user who is sending the message
to, the ID of the user who the message is to
msg, the message
date, the timestamp

The problem I'm having is trying to select the users who the user has sent or received messages from/to. I keep getting repeats or a user is missing. I had an idea of going through each user and going through validation to make sure that person isn't already there but it doesn't seem to work properly.

$id is from the session variable $_SESSION['id'] which is declared in an include file.

Any help would me most appreciated.

Here's my PHP code:

HTML Code:
$get_conversations = mysql_query("SELECT DISTINCT `uid`,`to` FROM `messages` WHERE `to`='$id' OR `uid`='$id'");
				if(mysql_num_rows($get_conversations)>0) {
					while($fetch_conversations = mysql_fetch_array($get_conversations)) {
						list($uid,$to) = $fetch_conversations;
						if($uid==$id) {
							$user = $to;
						if($to==$id) {
							$user = $uid;
						$user_array = array();
						if(!in_array($user, $user_array)) {
							array_push($user_array, $user);
// this bit is just where the $user is changed into their username
					foreach($user_array as $user) {
						$get_user = mysql_query("SELECT `name` FROM `users` WHERE `id`='$user'");
						while($fetch_user = mysql_fetch_array($get_user)) {
							list($name) = $fetch_user;
						echo "<li><input type=\"hidden\" value=\"$user\" />$name</li>\n";
				} else {
					//no convos
					echo "<li>No convos</li>";