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

Query error after server upgraded to new mysql version 8.0.34

提问人:Zac Ang 提问时间:9/6/2023 更新时间:9/7/2023 访问量:130

问:

我使用此代码 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

php mysql codeigniter

评论

0赞 aynber 9/6/2023
它将整个表变量作为单个名称传递。看看你的子句和不匹配的反引号。我不熟悉 CI,但应该有一种更简单的方法来连接表。FROM

答:

1赞 Liam 9/7/2023 #1

您的“连接”信息被包裹在反引号中,因为该函数不是用来处理的。from()

尝试按如下方式单独处理联接信息:getSelectedOneField

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
);
            

评论

0赞 Zac Ang 9/7/2023
完善。完美工作 非常感谢。