php - Codeigniter更新SQL问题

标签 php mysql sql codeigniter

从 codeigniter 3.1.0 更新到 3.1.9 后,当前网站的一个模块出现了一些问题,查看它输出的 SQL,这可能是因为 order_by 部分,但我可以从 SQL 错误中看到一些问题正在添加许多`,但不知道如何修复此输出..

有问题的代码块:

interface IssueFlags
{
 const FLAG_NONE       = 0x000;
 const FLAG_PINNED     = 0x001;        // issue is pinned, will always stick to the top of other issues
 const FLAG_PRIVATE    = 0x002;        // issue is limited only to staff and are not visible for others
 const FLAG_LOCKED     = 0x004;        // issue is locked and is not editable by anyone, only staff are allowed to edit
 const FLAG_CLOSED     = 0x008;        // issue is not opened to comments anymore, only staff are allowed to comment
}

public function get($id = false, $limit = false)
{
    if($id = array_filter((array)$id, 'is_numeric'))
        $this->db->where_in('bugtracker_issues.id', $id);

    if(is_numeric($limit) && !empty($limit))
        $this->db->limit($limit);
    elseif(is_array($limit) && count($limit) == 2)
        $this->db->limit($limit[0], $limit[1]);

    // Limit columns to get proper data and prevent duplicate columns name overwrite eachother
    $this->db->select('bugtracker_issues.*, account_data.nickname');

    // Also join a few columns of account data, so we won't need to query for each user too
    $this->db->join('account_data', 'bugtracker_issues.author = account_data.id', 'left');

    // This allow us to use functions when bulding our query
    $this->db->protect_identifiers = false;

    // Sort issues by update then created timestamp, at the end their id and descending, pinned ones always comes first
    $this->db->order_by('`flags` & ' . self::FLAG_PINNED . ' DESC, IFNULL(`update`, `create`) DESC, `id` DESC');
    $this->db->protect_identifiers = true; // turn it off, as its default state is disabled

    $query = $this->db->from('bugtracker_issues')->get();
    if(!$query || !is_object($query))
        return false;

    if(!$query->num_rows())
        return array();

    return $query->result_array();
}

发生的错误:

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 '1 DESC, IFNULL(`update`, `create`) DESC, `id` DESC LIMIT 20' at line 4

SELECT `bugtracker_issues`.*, `account_data`.`nickname` FROM `bugtracker_issues` LEFT JOIN `account_data` ON `bugtracker_issues`.`author` = `account_data`.`id` ORDER BY `flags`` &` 1 DESC, IFNULL(`update`, `create`) DESC, `id` DESC LIMIT 20

最佳答案

您的 ORDER BY 子句无效。

ORDER BY `flags`` &` 1 DESC, IFNULL(`update`, `create`) DESC, `id` DESC LIMIT 20

这在 SQL 中没有意义。

您需要修复 PHP 代码的该部分以生成正确的 ORDER BY 子句:

$this->db->order_by('`flags` & ' . self::FLAG_PINNED . ' DESC, IFNULL(`update`, `create`) DESC, `id` DESC');

应该写成:

$this->db->order_by('`flags` DESC, IFNULL(`update`, `create`) DESC, `id` DESC');

关于php - Codeigniter更新SQL问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53678110/

相关文章:

mysql - 使用顶级查询时出现sql语法错误

php - 将其发送到数据库时出现字符问题

GroupBy/Having 查询的 Java HQL 问题

php - 逃避 MySQL 注入(inject)和过滤 XSS 攻击尝试的正确方法

javascript - 第 42 行的 fatal error : Call to a member function execute() on boolean in C:\xampp\htdocs\website\adduser. php

mysql - 如何使用多重内连接进行分组

mySQL 将 varchar 转换为日期

sql - 在聚合函数 SQL Server 中选择 N 行

php - 如何在 Jquery 成功方法中获取 "last inserted id to database "?

php - PHP while 循环中的 MySQL 查询在第一个结果后返回 NULL