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

すべてのクエリで以下&&のように置き換え ますANDSQL

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

  • toRESERVEDキーワードfromであるため、バックティックでエスケープする必要があります。

元、

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メッセージWHEREto= $ this-> userid AND to_viewed= 0 AND to_deleted= 0 ORDER BY createdDESC

そして、あなたがクエリで言及しているのは予約キーワードかもしれないと思うので、その代わりにいくつかの差分フィールドを試してください。

于 2012-10-03T04:51:35.343 に答える