PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP-Mysql服务器错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我的SQL查询有问题.我正在创建一个类似Facebook的社交网站,并试图进行聊天.我在下面包含了错误消息.

您的sql语法有误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在’to =’2’&&附近使用to_viewed =’0’&&第1行的to_deleted =’0’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` datetiR_274_11845@E NULL ,
`to_vdate` datetiR_274_11845@E NULL ,
`from_ddate` datetiR_274_11845@E NULL ,
`to_ddate` datetiR_274_11845@E NULL ,
`created` datetiR_274_11845@E NOT NULL
) ENGINE = MYISAM ;

解决方法:

查询中发现的错误列表:

>&&在sql中应以AND编写.

例如

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

> to and from应该使用反引号进行转义,因为它是RESERVED Keyword.

例如

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

大佬总结

以上是大佬教程为你收集整理的PHP-Mysql服务器错误全部内容,希望文章能够帮你解决PHP-Mysql服务器错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: