0

im currently having problem with Codigniter

here is my code

    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{

    }
}

where i run the project i get this error

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

i have already surfed for a solution but i cant solve the problem

can someone help me?

1
  • its always helpful to use var_dump($var); or even better to use a debugger. The variable $query doesn't store the result. Commented Aug 6, 2020 at 14:21

3 Answers 3

1

You probably used the wrong variable. Try this:

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{

    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

$query->result_array() will return an array of rows from database which satisfies the query condition. If you need only one row, you have to modify it.

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

Comments

0

In the mysqli_result class

  • Represents the result set obtained from a query against the database.

Here Mostly use two types

  1. set of Array items
  2. single row

In the format of using mysqli_result Class their are two methods of using to extract the result in above mentioned categories:

`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`

Here is the simple solution in Php Codeigniter:-

I have also face this problem when calling to

1-example"-

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

then just write solution

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

like this:-

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

Problem Solved!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.