3

我的 sql 查询有问题。我正在创建一个像 facebook 这样的社交网站并尝试进行聊天。我在下面包含了错误消息。

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 'to = '2' && to_viewed = '0' && to_deleted = '0' ORDER BY created DESC' at line 1

这是我的 sql 查询

function getmessages($type=0) {
    switch($type) {
        case "0": $sql = "SELECT * FROM messages WHERE to = '".$this->userid."' && `to_viewed` = '0' && `to_deleted` = '0' ORDER BY `created` DESC"; break; // New messages
        case "1": $sql = "SELECT * FROM messages WHERE to = '".$this->userid."' && `to_viewed` = '1' && `to_deleted` = '0' ORDER BY `to_vdate` DESC"; break; // Read messages
        case "2": $sql = "SELECT * FROM messages WHERE from = '".$this->userid."' ORDER BY `created` DESC"; break; // Send messages
        case "3": $sql = "SELECT * FROM messages WHERE to = '".$this->userid."' && `to_deleted` = '1' ORDER BY `to_ddate` DESC"; break; // Deleted messages
        default: $sql = "SELECT * FROM messages WHERE to = '".$this->userid."' && `to_viewed` = '0' ORDER BY `created` DESC"; break; // New messages
    }
    $result = mysql_query($sql) or die (mysql_error());


    if(mysql_num_rows($result)) {
        $i=0;

        $this->messages = array();

        while($row = mysql_fetch_assoc($result)) {
            $this->messages[$i]['id'] = $row['id'];
            $this->messages[$i]['title'] = $row['title'];
            $this->messages[$i]['message'] = $row['message'];
            $this->messages[$i]['fromid'] = $row['from'];
            $this->messages[$i]['toid'] = $row['to'];
            $this->messages[$i]['from'] = $this->getusername($row['from']);
            $this->messages[$i]['to'] = $this->getusername($row['to']);
            $this->messages[$i]['from_viewed'] = $row['from_viewed'];
            $this->messages[$i]['to_viewed'] = $row['to_viewed'];
            $this->messages[$i]['from_deleted'] = $row['from_deleted'];
            $this->messages[$i]['to_deleted'] = $row['to_deleted'];
            $this->messages[$i]['from_vdate'] = date($this->dateformat, strtotime($row['from_vdate']));
            $this->messages[$i]['to_vdate'] = date($this->dateformat, strtotime($row['to_vdate']));
            $this->messages[$i]['from_ddate'] = date($this->dateformat, strtotime($row['from_ddate']));
            $this->messages[$i]['to_ddate'] = date($this->dateformat, strtotime($row['to_ddate']));
            $this->messages[$i]['created'] = date($this->dateformat, strtotime($row['created']));
            $i++;
        }
    } else {

        return false;
    }
}

这是我的数据库架构

CREATE TABLE `messages` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 255 ) NULL 
`message` TEXT NOT NULL ,
`from` INT( 11 ) NOT NULL ,
`to` INT( 11 ) NOT NULL ,
`from_viewed` BOOL NOT NULL DEFAULT '0',
`to_viewed` BOOL NOT NULL DEFAULT '0',
`from_deleted` BOOL NOT NULL DEFAULT '0',
`to_deleted` BOOL NOT NULL DEFAULT '0',
`from_vdate` DATETIME NULL ,
`to_vdate` DATETIME NULL ,
`from_ddate` DATETIME NULL ,
`to_ddate` DATETIME NULL ,
`created` DATETIME NOT NULL
) ENGINE = MYISAM ;

4

4 回答 4

1

在所有查询中替换 &&AND如下所示SQL

SELECT * FROM messages WHERE to = '".$this->userid."' AND `to_viewed` = '0' AND `to_deleted` = '0' ORDER BY `created` DESC
于 2012-10-03T04:21:17.350 回答
1

在您的查询中发现的错误列表:


  • &&应该像ANDSQL 一样编写。

前任,

SELECT * FROM messages WHERE `to` = '' AND ....
                                       ^ this one

前任,

SELECT * FROM messages WHERE `to` = ...
SELECT * FROM messages WHERE `from` = ...
                             ^ this one
于 2012-10-03T04:18:03.190 回答
1

尝试将“&&”更改为“AND”

于 2012-10-03T04:18:41.040 回答
0

SELECT * FROM messages WHERE to = $this->userid AND to_viewed= 0 AND to_deleted= 0 ORDER BY createdDESC

而且我认为在查询中提到的可能是保留关键字,因此请尝试一些差异字段而不是那个。

于 2012-10-03T04:51:35.343 回答