codeigniter 服务器升级到新mysql版本8.0.34后出现查询错误

vmjh9lq9  于 2023-09-28  发布在  Mysql
关注(0)|答案(1)|浏览(154)

我使用这个代码3年没有任何问题。今天服务器突然自动升级到新的mysql版本导致查询错误:

ERROR - 2023-09-06 23:38:40 --> Query error: 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 '.`package_id`
WHERE `o`.`status` = 3 AND `o`.`user_id` = '278'' at line 2 - Invalid query: SELECT SUM(p.investment_capital) AS abc
FROM `tbl_product_order o LEFT JOIN tbl_package_master p ON p`.`id =` `o`.`package_id`
WHERE `o`.`status` = 3 AND `o`.`user_id` = '278'
ERROR - 2023-09-06 23:38:40 --> Severity: error --> Exception: Call to a member function row_array() on boolean /home2/aekcom/public_html/zac-work/application/libraries/Common.php 94

以下是我的代码:

function getSelectedOneField($select, $table, $where) 
        {   
            $this->CI->db->select($select);
            $this->CI->db->from($table);
            
            $where2 = substr($where,6);
            $this->CI->db->where($where2);
        
            $query = $this->CI->db->get();
            $result = $query->row_array();
        
            if ($result) {
                return $result[$select];
            }
            
            return null; // Return an appropriate value if no result is found
        }



function getTotalInvested($user_id=0)
        {
            $total_invested= 0;
        
            $total_invested = $this->getSelectedOneField("SUM(p.investment_capital) AS abc", "tbl_product_order o LEFT JOIN tbl_package_master p ON p.id = o.package_id", "WHERE o.status = 3 AND o.user_id = '" . $user_id . "'");
            if($total_invested !=''){ 
                $total_invested;
            }
            return CURRENCY.@number_format($total_invested,2);
        }



$data['total_invested'] = $this->common->getTotalInvested($this->session->userdata('WDW_MEMBER_LOGIN_ID'));

如何修复此错误?
我已将SUM(p.investment_capital)更改为
SUM(p.investment_capital) AS abc

nx7onnlm

nx7onnlm1#

您的“join”信息被 Package 在反引号中,因为from()函数不打算处理它。
尝试在getSelectedOneField中单独处理join信息,如下所示:

function getSelectedOneField($select, $table, $where, $join_table, $join_condition) 
    // ...
    if($join_table && $join_condition)
    {
        $this->CI->db->join($join_table, $join_condition);
    }

然后这样称呼它:

$total_invested = $this->getSelectedOneField(
    "SUM(p.investment_capital) AS abc",
    "tbl_product_order o",
    "WHERE o.status = 3 AND o.user_id = '" . $user_id . "'",
    "tbl_package_master p", // $join_table
    "p.id = o.package_id" // $join_condition
);

相关问题