向连接的用户发送消息

I want to send a message from a user (user A) to another user (user B) which those users are connected each other in the database. To be more specific.

We keep the connection of the users in a table in the database which we called friends. In this table we have two columns, username and friend.

I have the code in order to send data between the users but it doesn't perform any checking in order to see if the user A who wants to send a message to the user B are connected to each other. If the users are connected I want to allow them to send the message and if they are not I want to echo a notification that they are not allowed to send a message because they are not connected.

I can understand that I want an if condition where I perform the check to see if the users are connected and have the appropriate code below and if it is not connected then output the notification described above.

How can I create this checking?

I am using php and mysql

HERE IS MY CODE...

<?php
include_once 'header.php';

if (!$loggedin) die();

if (isset($_GET['view'])) {
  $view = sanitizeString($_GET['view']);
} else {
  $view = $username;
}

if (isset($_POST['text'])){
    $text = sanitizeString($_POST['text']);

    if ($text != ""){
        $pm   = substr(sanitizeString($_POST['pm']),0,1);
        $time = time();
        queryMysql("INSERT INTO messages VALUES(NULL, '$username', '$view', '$pm', $time, '$text')");
    }
}

if ($view != "") {
    if ($view == $username) {
       $name1 = $name2 = "Your";
    } else {
        $name1 = "<a href='members.php?view=$view'>$view</a>'s";
        $name2 = "$view's";
    }

    echo "<div class='main'><h3>$name1 Messages</h3>";
    showProfile($view);

    echo <<<_END
<form method='post' action='messages.php?view=$view'>
Type here to leave a message:<br />
<textarea name='text' cols='40' rows='3'></textarea><br />
Public<input type='radio' name='pm' value='0' checked='checked' />
Private<input type='radio' name='pm' value='1' />
<input type='submit' value='Post Message' /></form><br />
_END;

    if (isset($_GET['erase'])) {
        $erase = sanitizeString($_GET['erase']);
        queryMysql("DELETE FROM messages WHERE id=$erase AND recip='$username'");
    }

    $query  = "SELECT * FROM messages WHERE recip='$view' ORDER BY time DESC";
    $result = queryMysql($query);
    $num    = mysql_num_rows($result);

    for ($j = 0 ; $j < $num ; ++$j) {
        $row = mysql_fetch_row($result);

        if ($row[3] == 0 || $row[1] == $username || $row[2] == $username) {
            echo date('M jS \'y g:ia:', $row[4]);
            echo " <a href='messages.php?view=$row[1]'>$row[1]</a> ";

            if ($row[3] == 0) {
                 echo "wrote: &quot;$row[5]&quot; ";
            } else { 
                 echo "whispered: <span class='whisper'>" . "&quot;$row[5]&quot;</span> ";
            }
            if ($row[2] == $username) {
                echo "[<a href='messages.php?view=$view" . "&erase=$row[0]'>erase</a>]";
            }
            echo "<br>";
        }
    }
}

if (!$num) {
  echo "<br /><span class='info'>No messages yet</span><br /><br />";
}
echo "<br /><a class='button' href='messages.php?view=$view'>Refresh messages</a>";
?>

</div><br /></body></html>

The checking system for my question is the below and it works..

<?php
include_once 'header.php';

if (!$loggedin) die();

if (isset($_GET['view'])) $view = sanitizeString($_GET['view']);
else                      $view = $username;

$result1 = mysql_num_rows(queryMysql("SELECT username,friend FROM friends
            WHERE username='$username' AND friend='$view'"));



$result2 = mysql_num_rows(queryMysql("SELECT username,friend FROM friends
            WHERE username='$view' AND friend='$username'"));

if (($result1 + $result2) > 1)
{
 //REST OF THE CODE 
}

?>

what we are doing is that for the result1 we are checking if the logged in username($username) is connected with the viewed profile ($view) and for the result2 we are doing vice versal, to be more specific we are checking in the result2 that if the username of the viewed profile($view) is connected with the ($username) then in the if statement we check that if those two result has more than one row in the table then they are both connected.

PS: sorry for my bad english