mamba08
mamba08

Reputation: 23

CodeIgniter Select Statement with Where clause

Hi I'm new to CodeIgniter and I just want to know How will I query from my MySql Db, a Select Statement with a where clause, I know it can be searched from the net but whenever I try something I get errors, It's really frustrating. The string in the Where clause will be coming from a User Input. Thanks guys!

Upvotes: 0

Views: 29653

Answers (4)

KUMAR
KUMAR

Reputation: 1995

In Codeigniter with Method Chaining Style :-

 $data['getData'] = $this->db->get_where('table_name',array('column_name'=>$var))->result_array();

Upvotes: 0

Waseem shah
Waseem shah

Reputation: 450

Try this one.

$id = 'your id';
$this->db->select("*");
$this->db->from("table_name");
$this->db->where('id','$id');
$query = $this->db->get();
return $query->result_array();

Upvotes: 0

Craig Barben
Craig Barben

Reputation: 148

You can do as Mehedi-PSTU stated, however it seems as though you're a little new to this, so here's some extra information:

I'll copy Mehedi-PSTU for the most part here.

$this->get->where('column_name', $equals_this_variable);
$query = $this->db->get('table_name');

This will store the query object in the variable $query. if you wanted to convert that to a usable array, you just perform to following.

$results = $query->result_array();

Or you can loop through it like this:

foreach($query->result_array() as $result){
    // Perform some task here.
}

A better or even full understanding can probably come from:

http://ellislab.com/codeigniter/user-guide/database/active_record.html

Upvotes: 2

MH2K9
MH2K9

Reputation: 12039

Try something like this

    $this->db->where('db_attr', $var);
    return $this->db->get('table');

Upvotes: 0

Related Questions