0

I am using codeigniter for my project and couldn't find what's wrong with my code. This is my controller code

$products = $this->products_model->getProductsQuantity($id);
$q = $warehouse_product->quantity;

object notation is working but getting error: Trying to get property of non-object

$q = $warehouse_product['quantity'];

array notation not working and getting php error: Fatal error: Cannot use object of type stdClass as array in products\controllers\products.php on line xx

Here is my model function

public function getProductsQuantity($product_id) 
    {
        $q = $this->db->get_where('products', array('product_id' => $product_id), 1); 
          if( $q->num_rows() > 0 )
          {
            return $q->row();
          } 

          return FALSE;

}

Please help me to fins where is the problem in my code.

1 Answer 1

1

You should look at this

if you are limiting your results to 1 as in your get_where instruction then return

$q->row_array() // for array

And

$q->result_array() // for object

And in Controller do this

$products = $this->products_model->getProductsQuantity($id);
$quantity = $products->quantity; // Object notation

And

$quantity = $products['quantity']; // Array notation
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Raheel, row_array() worked with array notation but $q->result_array() for object notation didn't.
for object notation it is row() sorry for that take a look at this codeigniter.com/user_guide/database/results.html

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.