phpcodeigniteractiverecordwhere-clausequery-builder

CodeIgniter active record query not respecting where() calls after the get() call


if use of this code, show me all data, all names and all ids. what do i do?
i use of codeIgniter

With respect

$search_customer = 1;//$this->input->post('search_customer');
$where = "id=$search_customer OR name=$search_customer";
$query = $this->db->get('customer');
$this->db->where('id', $search_customer);
$this->db->or_where('name', $search_customer);
if ($query->num_rows() == 0) {
    echo '0';
} else {
    $data = array();
    foreach ($query->result() as $row)
    {
        $data[] = $row;
    }
    echo json_encode($data);
}

Solution

  • This line runs the query:

    $query = $this->db->get('customer');
    

    before you have set your where clauses. You probably want

    $this->db->where('id', $search_customer);
    $this->db->or_where('name', $search_customer);
    $query = $this->db->get('customer');
    

    If you are still having problems take a look at the sql being generated/run:

    echo $this->db->last_query();