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

I've read the docs about these two functions, but I still can't seem to get the difference between the two.

I understand that it get_where

is fetching data from the DB, but when should we use the function where()

instead get_where()

?

+3


source to share


2 answers


get_where ()

There are many other ways to get data using the CodeIgniters 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()

- the naive case and of course the most frequently used in my code anyway - I can't think of another structure in any other language that allows you to be productive with data with one line of code.

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

      

Parameters:

  • $ table (mixed) - table for data retrieval; string or array
  • $ where (string) is a WHERE clause
  • $ limit (int) - LIMIT clause
  • $ offset (int) - OFFSET clause

This function works like get()

but also allows you to add WHERE

directly.



Identical $this->db->get();

except that it allows you to add a WHERE

in the second parameter instead of using db->where()

.

Where()

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

You can also add where conditions, sorting conditions, etc:

$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 can tie all of these conditions together on one line, but I prefer to put them on separate lines for readability.

In a simple word get_where

, it's a luxury, but where()

it gives you a lot of flexibility to use.

+4


source


get_where

is a combined function - to talk about functions where()

and get()

,

according to the documentation:

$ this-> db-> get_where ()

Identical to the above function, except that it allows you to add "where" in the second parameter, instead of using db-> where () function

also, looking at the source code get_where()

, you will notice that



if ($where !== NULL)
{
    $this->where($where);
}

      

where $where

is the second parameter of the method get_where()

.

In simple words, it $this->db->get_where('table name', 'where clause')

is an alias for$this->db->where('where clause')->get('table name');

+1


source







All Articles