active record class in codeigniter (Select) Part: 3
<?php
//Custom string:
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> like('column_name', 'match');
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name LIKE '%match%'
// If anyone use multiple function calls they will be chained together with AND between them:
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> like('column_name1', 'match1');
$this -> db -> like('column_name2', 'match2');
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name1 LIKE '%match1%'
//AND column_name2 LIKE '%match2'
//Like Use an Optional third argument
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> like('column_name', 'match','before');
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name LIKE '%match'
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> like('column_name', 'match','after');
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name LIKE 'match%'
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> like('column_name', 'match','both');
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name LIKE '%match%'
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> like('column_name', 'match','none');
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name LIKE 'match'
//Associative array method:
$array = array('column_name1' => $variable1,'column_name2' => $variable2);
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> like($array);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name1 LIKE '%variable1%'
//AND column_name2 LIKE '%variable2%';
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> like('column_name1',$variable1);
$this -> db -> or_like('column_name1',$variable1);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name1 LIKE '%variable1%'
//OR column_name2 LIKE '%variable2%';
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> like('column_name1',$variable1);
$this -> db -> or_not_like('column_name1',$variable1);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename where column_name1 LIKE '%variable1%'
//OR column_name2 NOT LIKE '%variable2%';
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> group_by("column_name");
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename GROUP BY column_name
//Pass multiple value
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> group_by(array("column_name1","column_name2"));
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename GROUP BY column_name1,column_name2
$this -> db -> distinct();
$query = $this -> db -> get('tablename');
//Produces : SELECT DISTINCT * FROM tablename;
$this -> db -> having('column_name1 = $variable1');
$query = $this -> db -> get('tablename');
//Produces : SELECT * FROM tablename HAVING column_name1 = $variable1;
$this -> db -> having('column_name1', $variable1);
$query = $this -> db -> get('tablename');
//Produces : SELECT * FROM tablename HAVING column_name1 = $variable1;
//Pass an array of multiple values
$this -> db -> having(array('column_name1 =' => $variable1, 'column_name2 <' => $variable2));
$query = $this -> db -> get('tablename');
//Produces : SELECT * FROM tablename HAVING column_name1 = $variable1,
//column_name2 < $variable2;
//If you are using a database that CodeIgniter escapes queries for,
//you can prevent escaping content by passing an optional third
//argument, and setting it to FALSE.
$this -> db -> having('column_name', $variable);
// Produces: HAVING 'column_name' = $variable in some databases such as MySQL
$this -> db -> having('column_name', $variable, FALSE);
// Produces: HAVING column_name = $variable
$this -> db -> or_having('column_name', $variable);
// Produces: HAVING 'column_name' = $variable in some databases such as MySQL
//Order By Clause
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> order_by("column_name","desc");
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename ORDER BY column_name DESC
//Pass own string in the first parameter
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> order_by('column_name1 asc, column_name2 desc');
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename ORDER BY column_name1 ASC, column_name2 DESC
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> order_by("column_name1 asc");
$this -> db -> order_by("column_name2 desc");
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename ORDER BY column_name1 ASC, column_name2 DESC
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> limit(10);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename LIMIT 10
$this -> db -> select('*');
$this -> db -> from('tablename');
$this -> db -> limit(10, 20);
$query = $this -> db -> get();
//Produces : SELECT * FROM tablename LIMIT 20, 10
echo $this -> db -> count_all_results('tablename');
//Produces : an integer like 20
echo $this -> db -> count_all('tablename');
//Produces : an integer like 20
?>
Comments
Post a Comment