【问题标题】:Error Number: 1064 CodeIgniter query builder错误号:1064 CodeIgniter 查询生成器
【发布时间】:2020-01-06 17:43:44
【问题描述】:

您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 6 行的 ') AND instrument.type = 2 ORDER BY instrument.price DESC LIMIT 100' 附近使用正确的语法

SELECT 
    `instrument`.`id` as `id`, 
    `instrument`.`name` as `name`, 
    `price`, 
    `isix`, 
    `wkn`, 
    `isin`, 
    `instrument`.`account` as `accID`, 
    `own_invest`, 
    `interest`, 
    (select company 
        from chart_of_accounts 
        where chart_of_accounts.id = accID 
        group by company
    ) as company_id, 
    (select url 
        from company 
        where company.id = company_id
    ) as company_url, 
    `tiv_bond_type`.`name` as `bond_type` 
FROM 
    `instrument` 
LEFT JOIN `bond` ON `bond`.`instrument` = `instrument`.`id` 
LEFT JOIN `tiv_bond_type` ON `tiv_bond_type`.`bond_type` = `bond`.`type` 
WHERE 
    ( ) AND 
    `instrument`.`type` = 2 
ORDER BY 
    `instrument`.`price` DESC LIMIT 100

文件名:models/Search_model.php

行号:251

public function search_bonds($name, $isin, $isix, $wkn, $type, $initial_price, $end_price, $initial_interest, $end_interest) {
        $bond_name = explode(" ", $name);

        $this->db->select("instrument.id as id, instrument.name as name, price, isix, wkn, isin, instrument.account as accID, own_invest, interest,
                            (select company from chart_of_accounts where chart_of_accounts.id = accID group by company) as company_id,
                            (select url from company where company.id = company_id) as company_url,
                            tiv_bond_type.name as bond_type");
        $this->db->from("instrument");
        $this->db->join("bond", "bond.instrument = instrument.id", "left", "outer");
        $this->db->join("tiv_bond_type", "tiv_bond_type.bond_type = bond.type", "left");
        if (strlen($isin) > 3)
            $this->db->where("instrument.isin", $isin);

        if (strlen($name) > 1) {
            $this->db->group_start();
            $this->create_search_permutations($bond_name, 1);
            $this->db->group_end();
        }

        if (strlen($isix))
            $this->db->where("instrument.isix", $isix);

        if (strlen($wkn) > 3 )
            $this->db->where("instrument.wkn", $wkn);

        if (strlen($type) && is_numeric($type))
            $this->db->where('bond.type', $type);

        if (is_numeric($initial_price) && is_numeric($end_price)) {
            $this->db->group_start();
            $this->db->where("instrument.price >= ", $initial_price);
            $this->db->where("instrument.price <= ", $end_price);
            $this->db->group_end();
        }
        else if (is_numeric($initial_price) && ! is_numeric($end_price))
            $this->db->where("instrument.price >= ", $initial_price);
        else if (is_numeric($end_price)  && ! is_numeric($initial_price))
            $this->db->where("instrument.price <= ", $end_price);

        if (is_numeric($initial_interest) && is_numeric($end_interest)) {
            $this->db->group_start();
            $this->db->where("bond.interest >= ", $initial_interest);
            $this->db->where("bond.interest <= ", $end_interest);
            $this->db->group_end();
        }
        else if (is_numeric($initial_interest) && ! is_numeric($end_interest))
            $this->db->where("bond.interest >= ", $initial_interest);
        else if (is_numeric($end_price)  && ! is_numeric($initial_interest))
            $this->db->where("bond.interest <= ", $end_interest);
        $this->db->where("instrument.type", 2);
        $this->db->order_by("instrument.price", "desc");
        $this->db->limit(100);
        return $this->db->get()->result_array();
    }

        private function create_search_permutations($search_array, $table_name, $permutations = array()) {
        /*
            $query_type variable is defining the type of table column the query string is targeting
            ie $query_type == 1 defines that the query string is for instrument.name and $query_type == 2 is for company.name
        */
        if (empty($search_array)) {
            if ($table_name == 'instrument')
                $this->db->or_where("instrument.name like '%".join('%', $permutations)."%'");
            elseif ($table_name == 'company')
                $this->db->or_where("company.name like '%".join('%', $permutations)."%'");
            else
                return;
        }
        else {
            for ($iterator = count($search_array) - 1; $iterator >= 0; --$iterator) {
                $new_search_array = $search_array;
                $new_permutations = $permutations;
                list($key) = array_splice($new_search_array, $iterator, 1);
                array_unshift($new_permutations, $key);
                $this->create_search_permutations($new_search_array, $table_name, $new_permutations);
            }
        }
    }

任何人都可以帮助找出我的代码有什么问题。

【问题讨论】:

    标签: php mysql codeigniter mariadb codeigniter-3


    【解决方案1】:

    我的猜测是问题出在create_search_permutations 方法上。该方法包含在 group_start/group_end 对中,但实际上对查询没有任何作用。

    【讨论】:

    • 那么你对这个方法有什么建议应该删除或者如果你要解决这个错误你会怎么做。
    • 我认为这正是问题所在...group_startgroup_end 正在将打开和关闭括号添加到 SQL 文本中。并且生成的 SQL 无效,即。 ... WHERE ( ) AND ... 无效。至于修复代码,删除那些调用。如果需要,create_search_permutations 应该负责添加左括号和右括号,如果没有向 SQL 文本添加任何内容,则不添加括号。
    • 真的,我相信这是原因,请澄清我将如何在 create_search_permutations 方法中添加 group_start 和 group_end 或者我应该完全删除它们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多