2012-10-03 93 views
3

我遇到了我的sql查詢問題。我正在創建像Facebook這樣的社交網站,並試圖進行聊天。我在下面列出了錯誤消息。PHP - Mysql服務器錯誤

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' 在行1

這裏ORDER BY created DESC」是我的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 ; 

回答

1

列表找到的錯誤在您的查詢中:


  • &&應寫在SQL AND

前,

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

前,

SELECT * FROM messages WHERE `to` = ... 
SELECT * FROM messages WHERE `from` = ... 
          ^this one 
+0

試圖把反向()和(從),但它給了我這個錯誤未知的列'到'在'where子句' –

+0

確實列'to'出現在您的表'messages'?你可以給全表格模式嗎? –

+0

編輯帖子 –

1

嘗試改變 「& &」 to 「和」

1

更換&&AND像下面所有SQL查詢

SELECT * FROM messages WHERE to = '".$this->userid."' AND `to_viewed` = '0' AND `to_deleted` = '0' ORDER BY `created` DESC 
0

SELECT * FROM信息在哪裏= $ this-> userid AND to_viewed = 0 AND to_deleted = 0 ORDER BY created DESC

而且我覺得,你是在一個查詢可能可以保留關鍵字,以便嘗試一些差異場的而不是提。