我的 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 ;