Codeigniter:error无法将CI_DB_mysqli_result类型的对象用作数组



im当前与Codimitter 存在问题

这是我的代码

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:xampphtdocsindexstockapplicationmodelsModel_products.php

我已经在网上寻找解决方案,但我不能解决的问题

有人能帮我吗?

您可能使用了错误的变量。试试这个:

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{
}
}

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

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{
}
}

在mysqli_result类中

  • 表示从对数据库的查询中获得的结果集

这里主要使用两种类型的

  1. 数组项集
  2. 单行

在使用mysqli_result Class的格式中,有两种方法用于提取上述类别中的结果:

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

问题解决了!

相关内容

  • 没有找到相关文章

最新更新