MySQL - WHERE,AND&或者语法

时间:2012-08-29 12:33:29

标签: php mysql chat

我正在编写一个私人聊天的东西,但现在我遇到了这个问题:
您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'to ='gskartwii'附近使用正确的语法,或者在第1行使用'='gskartwii'AND to ='gs'' 警告:mysql_fetch_assoc()要求参数1为资源,在第38行的C:\ xampp \ htdocs \ rivase \ private.php中给出布尔值
这是我的代码:

<!-- language: php -->
<?php
$result=mysql_query("SELECT * FROM rivase_chat_posts WHERE sender='$user' AND to='$user2' OR sender='$user2' AND to='$user'");
echo mysql_error();
while ($row = mysql_fetch_assoc($result)) {
    $sender=$row['sender'];
    $content=$row['content'];
    $time=$row['time'];
    echo "$sender : $content <span class='hidden'>$time</span><br />\n";
}
?>

感谢大家的快速回复!

2 个答案:

答案 0 :(得分:10)

尝试使用反引号转义to

$result=mysql_query("SELECT * FROM rivase_chat_posts 
                    WHERE sender='$user' AND `to`='$user2' 
                    OR sender='$user2' AND `to`='$user'");

since it is a reserved word in MySQL

答案 1 :(得分:1)

TO是reserved word

$result=mysql_query("SELECT * FROM rivase_chat_posts WHERE sender='$user' AND `to`='$user2' OR sender='$user2' AND `to`='$user'");

应该工作。