Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use codeigniter where() function and when get_where()

Tags:

codeigniter

I've been reading docs regarding these 2 functions, but I still can't quite get the difference between these two.

I get it that get_where selects data from DB, but when should we use where() function and not get_where()?

like image 296
Evgeny Pavlov Avatar asked Mar 08 '23 11:03

Evgeny Pavlov


1 Answers

get_where()

There are tons of other ways to get data using CodeIgniter’s ActiveRecord implementation, but you also have full SQL queries if you need them: Example:

$query = $this->db->get_where('people', array('id' => 449587));

Ultimately, get_where() is the naive case, and certainly the most commonly-used in my code anyway — I can’t think of an another framework in any other language that enables you to be this productive with data with a single line of code.

get_where([$table = ''[, $where = NULL[, $limit = NULL[, $offset = NULL]]]])

Parameters:

  • $table (mixed) – The table(s) to fetch data from; string or array
  • $where (string) – The WHERE clause
  • $limit (int) – The LIMIT clause
  • $offset (int) – The OFFSET clause

This function is working as get() but with also allows the WHERE to be added directly.

Identical to the $this->db->get(); except that it permits you to add a where clause in the second parameter, instead of using the db->where() function.

where()

This function enables you to set WHERE clauses in your query.

You can also add where clauses, sort conditions and so forth:

$this->db->select('first_name', 'last_name');
$this->db->from('people');
$this->db->where('id', 449587);
$this->db->order_by('last_name, first_name');
$query = $this->db->get();

It’s possible to chain all these conditions together on a single line, but I prefer putting them on separate lines for readability.

In simple word, get_where is a luxury to use but where() gives you more flexibility to use.

like image 135
always-a-learner Avatar answered Apr 01 '23 16:04

always-a-learner