0
votes

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!

4
Can you share some of the code you've tried with the errors you're getting? This stuff should all be covered in the CI manual - scrowler
$expiry = $this->db->query("SELECT abc FROM abc_db WHERE abc_id = '$abc' "); $expiry2 = $expiry->results(); - mamba08
i get Call to undefined method CI_DB_mysql_result::results() - mamba08
sorry for the question guys - mamba08
because there is no results() only result() please refer to the documentation ellislab.com/codeigniter/user-guide/database/index.html - tomexsans

4 Answers

2
votes

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

0
votes

Try something like this

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

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

In Codeigniter with Method Chaining Style :-

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