0

How do I structure the below query in Codeigniter. I can't seem to figure it out.

This is the function in my model:

public function update_daily_inventory()
{
    foreach ($this->parseInventoryHtml() as $item) {    
        $_item = $this->db->query('SELECT COUNT(*) as `count` FROM product WHERE product_code = "' . $item['sku'] . '"');
    }
    return $_item;
}

I want to use an array key as the where variable. Apologize if this is simple I am new to coding. This keeps returning 0.

0

3 Answers 3

2
$this->db->where('product_code', $item['sku']);
$this->db->select('COUNT(*) as `count`');
$_item =  $this->db->get('product')->row()->count;
Sign up to request clarification or add additional context in comments.

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
0

There is no need to set alias for count.here is simple query to count number of rows found in result.

$this->db->where('product_code', $item['sku']);
$this->db->select('*');
$_item =  $this->db->get('product')->num_rows();

1 Comment

If you only need the count of the rows, then don't select all of the columns to be included in the result set.
0

You can use below code to structure your query.

$this->db->select('*');
$this->db->where('product_code', $item['sku']);
$_item = $this->db->count_all_results('product');  

Hope this helps.

1 Comment

select('*') is a pointless call if you are going to execute the build query with count_all_results().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.