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

推荐答案

您的"Join"信息被放在反号中,因为from()函数不是用来处理这一点的.

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

Php相关问答推荐

PHP -使用preg_match在字符串中进行匹配

在不刷新页面的情况下无法使用JQuery更新id

允许Laravel路由指定`withTrashed`,尽管有显式的模型绑定

自定义WooCommerce帐户下载列表

Msgraph-sdk-php v2如何从返回对象中获取数据?

PHP -多维数组到一维数组

PHP测试字符串的所有POST值

这是在可召回的背景下吗?

如何在PHP中组合日期范围

随机显示WordPress快捷代码功能时出现问题

如何在 php 中获取正确的时间

Laravel Eloquent:复杂的多对多关系以及缺失关系的默认值

防止使用woocommerce_checkout_process下单

基于WooCommerce中的产品变体自定义感谢页面跳转

为WooCommerce中的运输方式ID设置必填的 checkout 订单备注字段

从图像URL中获取文件扩展名?

在WooCommerce中显示 checkout 的高级自定义字段(Advanced Custom Fields)并保存其值

Laravel:ArgumentCountError: 函数 App\Mail 的参数太少

使用来自 PHP 表单的数据更新 xml 命名空间

如何在不解压缩/重新压缩的情况下连接(连接)两个缩小的值