PHP - Mysql 服务器错误

标签 php mysql

我的 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' && <code>to_viewed</code> = '0' && <code>to_deleted</code> = '0' ORDER BY <code>created</code> 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;
    }
}

这是我的数据库架构

<pre><code>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 ; </code></pre>

最佳答案

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


  • &&在SQL中应该写成AND

例如,

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

例如,

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

关于PHP - Mysql 服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12701822/

相关文章:

php - 将键和值插入关联数组

php - 当 key 不匹配时,如何判断 phpseclib sftp 响应是否是密码请求的质询

php - 无法加载 phpMyAdmin 配置(./config.inc.php :31): parse error

php - 用于在多对多查找表中添加/删除条目的 HTML 复选框 (PHP/MySQL)

PHP游戏设计-获取数据库信息

MySQL 命令行客户端崩溃

php - 将索引重置为基于 1 的数组

php - 用 Dotrine 进行日期查询

php - 如何使用 mysql 在 android 中更改密码(注册和登录工作正常,但更改密码时出现问题)

mysql - 这个查询可以写在 MS SQL 服务器上吗?该查询适用于 MySQL,但在 MS SQL Server 上未提供所需的输出