Kimberly Mullins
Kimberly Mullins

Reputation: 101

problem with where in codeIgniter

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);
        }

Upvotes: 0

Views: 2568

Answers (3)

mattumotu
mattumotu

Reputation: 1514

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();

Upvotes: 0

Richard Testani
Richard Testani

Reputation: 1482

Looks like just as the error is telling you. You don't have a column in your database named 'id=1'

Try using an array

$array = array('id'=>$search_customer);
$this->db->get_where('customers', $array);

http://codeigniter.com/user_guide/database/active_record.html#select

There's also or_where available:

$this->db->or_where('name', $search_customer);

Upvotes: 1

Eduardo Reveles
Eduardo Reveles

Reputation: 2175

Try this for the conditions:

$this->db->where('id', $search_customer);
$this->db->or_where('name', $search_customer);

You can see on the docs that using get_where you use an associative array.

Upvotes: 1

Related Questions