codeigniter 代码触发器:错误无法将CI_DB_mysqli_result类型的对象用作数组

ki1q1bka  于 2022-12-06  发布在  Mysql
关注(0)|答案(3)|浏览(117)

我目前遇到Codigniter问题
这是我代码

public function move($data)
{
    $sku = $data['sku'];
    $store = $data['store_id'];
    $sql = "SELECT * FROM products WHERE sku = ? AND store_id = ?";
    $query = $this->db->query($sql, array($data['sku'], $data['store_id']));

    if($query->result_array() != "") {
        $data['qty'] =  $data['qty'] + $query['qty'] ;  //Error here
        $insert = $this->db->query('products', $data);
        return ($insert == true) ? true : false;
    }
    else{

    }
}

在运行项目时出现此错误

Type: Error    
Message: Cannot use object of type CI_DB_mysqli_result as array
Filename: C:\xampp\htdocs\index\stock\application\models\Model_products.php

我已经上网寻找解决方案,但我无法解决这个问题
谁能帮帮我?

eimct9ow

eimct9ow1#

您可能使用了错误的变量。请尝试以下操作:

public function move($data)
{
    $sku = $data['sku'];
    $store = $data['store_id'];
    $sql = "SELECT * FROM products WHERE sku = ? AND store_id = ?";
    $query = $this->db->query($sql, array($data['sku'], $data['store_id']));
    $result_array = $query->result_array();
    if($result_array) {
        $data['qty'] =  $data['qty'] + $result_array['qty'] ;  //Error here
        $insert = $this->db->query('products', $data);
        return ($insert == true) ? true : false;
    }
    else{

    }
}
wlwcrazw

wlwcrazw2#

$query-〉result_array()将从数据库中返回一个满足查询条件的行数组。如果你只需要一行,你必须修改它。

public function move($data)
{
    $sku = $data['sku'];
    $store = $data['store_id'];
    $sql = "SELECT * FROM products WHERE sku = ? AND store_id = ?";
    $query = $this->db->query($sql, array($data['sku'], $data['store_id']));

if($query->num_rows() > 0){
    $row = $query->row();  // select only first row of the query result
    $data['qty'] =  $data['qty'] + $row['qty'] ;
    $insert = $this->db->query('products', $data);  // don't know what you are doing here
    return ($insert == true) ? true : false;
}else{
}
}
jqjz2hbq

jqjz2hbq3#

在mysqli_result类中

    • 表示从数据库查询中获取的结果集。*

这里主要使用两种类型
1.数组项集
1.单行
在使用mysqli_result类的格式中,有两种方法用于提取上述类别中的结果:

`1-mysqli_result::fetch_array — Fetch the next row of a result set as an associative, a numeric array, or both`

`2-mysqli_result::fetch_row — Fetch the next row of a result set as an enumerated array`

下面是PHP Codeigniter中的简单解决方案:-
我打电话给

1-示例”-

$registrations = $this->db->get_where('registration', array('school_id' => school_id()))

那就写"解决方案"

->result_array(); or ->row_array();

就像这样:-

$registrations = $this->db->get_where('registration', array('school_id' => school_id()))->result_array();

问题已解决!

相关问题