2015-11-28 01:18
浏览 48


I'm creating a messaging system for my site and i'm trying to display your friends and i'm having trouble as i'm getting the error:

Select Friend: Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT `user_two` FROM `friends` WHERE (`user_one`='2')' at line 1

Heres My PHP Code:

mysql_connect("************", "*****", "**********");

if(isset($_GET['user']) && !empty($_GET['user'])){
   echo "Send Message To " . getuser($_GET['user'], 'first_name');
} else {
   echo "Select Friend: ";
$my_id = $_SESSION['user_id'];
$friend_list = mysql_query("SELECT `user_one` FROM `friends` WHERE (`user_two`='$my_id') OR SELECT `user_two` FROM `friends` WHERE (`user_one`='$my_id')");
if (!$friend_list) { // add this check.
      die('Invalid query: ' . mysql_error());
while($run_user = mysql_fetch_array($friend_list)){
    if($run_user['user_one'] == $my_id){
        echo "1";
    } else {
        echo "2";

And heres my friends table structure:

`user_one` int(11)   
`user_two` int(11) 

I'm trying to get the friends ID and make sure it isn't mine and then display the information i need to, i am a little bit rusty with SQL so just have that in mind.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhong8856 2015-11-28 01:33

    Just remove the brackets on the inside of the query...done

    点赞 打赏 评论
  • douyi4991 2015-11-28 02:03

    First, you shouldn't use mysql_. It is deprecated. Use mysqli.

    Your query is:

    SELECT `user_one`
    FROM `friends`
    WHERE (`user_two`='$my_id') OR
    SELECT `user_two`
    FROM `friends`
    WHERE (`user_one`='$my_id')

    This doesn't make sense. OR is not used to connect two queries. One solution is to use union all:

    SELECT `user_one`
    FROM `friends`
    WHERE `user_two` = '$my_id'
    SELECT `user_two`
    FROM `friends`
    WHERE `user_one` = '$my_id';
    点赞 打赏 评论

相关推荐 更多相似问题