active record class in codeigniter (Select) Part: 2
<?php
//Simple key/value method:
//Give oppertunity for conditional query
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> where('column_name',$variable);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name = '$variable';
//Where $variable is predifined
//If you use multiple function calls then they will be changed together with AND between them
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> where('column_name1',$variable1);
$this -> db -> where('column_name2',$variable2);
$this -> db -> where('column_name3',$variable3);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name1 = '$variable1'
//AND column_name2 = '$variable2' AND column_name3 = '$variable3';
//Where $variable1, $variabl2 and $variable3 is predifined
//Custom key/value method:
// You can include an operator in the first paramenter in order to control the comparison
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> where('column_name1 !=',$variable1);
$this -> db -> where('column_name2 <',$variable2);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name1 != '$variable1'
//AND column_name2 < '$variable2';
//Where $variable1 and $variable2 is predifined
//Associative array method:
$array = array('column_name1' => $variable1, 'column_name1' => $variable2);
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> where($array);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name1 = '$variable1'
//AND column_name2 = '$variable2';
//Where $variable1 and $variable2 is predifined
// You can include your own operators using this method as well
$array = array('column_name1 !=' => $variable1, 'column_name1 <' => $variable2);
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> where($array);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name1 != '$variable1'
//AND column_name2 < '$variable2';
//Where $variable1 and $variable2 is predifined
//Custom string:
$array = "column_name1 = 'Amitav' AND column_name2 = 'Dev' ";
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> where($array);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name1 = 'Amitav'
//AND column_name2 = 'Dev';
//Third Parameter of where()
//$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter //will not try to protect your field or table names with backticks.
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> where('column_name1 !=', $variable1);
$this -> db -> or_where('column_name2 >', $variable2);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name1 != $variable1
// OR column_name2 > $variable;
//Custom string:
$names = array('Amitav','Dev','Joy');
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> where_in('column_name', $names);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name IN ('Amitav','Dev','Joy')
//Custom string:
$names = array('Amitav','Dev','Joy');
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> or_where_in('column_name', $names);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where OR column_name IN ('Amitav','Dev','Joy')
//Custom string:
$names = array('Amitav','Dev','Joy');
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> where_not_in('column_name', $names);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name NOT IN ('Amitav','Dev','Joy')
//Custom string:
$names = array('Amitav','Dev','Joy');
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> or_where_not_in('column_name', $names);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where OR column_name NOT IN ('Amitav','Dev','Joy')
?>
Comments
Post a Comment