1

I have a JOIN query in CodeIgniter which returns an empty array.

My Controller part:

if ($this->session->has_userdata('user')) {
    $id = $this->session->user['id'];
    $where = ["products.user_id =" => $id];
    $status = $this->insertModel->get_status($where);
    $this->load->view('profile', ["status" => $status]);
}

My model:

return $this->db
    ->from('photos')
    ->join('products', 'photos.prod_id = products.id', 'left')
    ->where($where)
    ->get()
    ->result_array();
1
  • In my local environment running CI3.1.11, your model script is valid and runs as expected. We don't know your version. We dont know if this is a code problem or a data problem. Is the session value definitely populated and accessible or are you getting a null value? Have you tried $this->session->userdata('id');? It is true that you don't need to explicitly mention the = but it is also harmless. This question Needs Debugging Details. Commented yesterday

2 Answers 2

2

in your controller, just send $id instead of $where

$status = $this->insertModel->get_status($id);

and rebuild your where clause in your model the Codeigniter way:

->where('products.user_id', $id)

see the docs here

and about MVC (Model=database interaction, View=browser output and Controller=your application logic)

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

Comments

0

Controller:
Your error is in $where, check my code

if ($this->session->has_userdata('user')) {

    $id = $this->session->user['id'];

    //$where = ["products.user_id =" => $id];//old line
    $where = array("products.user_id" => $id);//new line

    $status = $this->insertModel->get_status($where);

    $this->load->view('profile', ["status" => $status]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.