-
Notifications
You must be signed in to change notification settings - Fork 2
Database
saturngod edited this page Mar 23, 2012
·
9 revisions
Loading the database library
$this->db->load('db');
$this->db->get('table');
in system/config/development.php ( for production : system/config/development.php )
public static $autoload=array("db");
SELECT id FROM table
code will be
$result = $this->db->select('id')->get('table');
if($this->db->count() > 0) {
echo $result[0]->id;
}
SELECT DISTINCT('name') FROM table
code will be
$result = $this->db->distinct('name')->get('table');
if($this->db->count() > 0) {
echo $result[0]->id;
}
SELECT * FROM table WHERE id = 5
code will be
$this->db->where("id","5");
$result = $this->db->get("table");
if($this->db->count() > 0) {
echo $result[0]->id;
}
SELECT * FROM table WHERE id = 5 AND name = 'sample'
code will be
$this->db->where("id","5");
$this->db->where("name","sample");
$result = $this->db->get("table");
if($this->db->count() > 0) {
echo $result[0]->id;
}
SELECT * FROM table WHERE id = 5 OR name = 'sample'
code will be
$this->db->where("id","5");
$this->db->where_or("name","sample");
$result = $this->db->get("table");
if($this->db->count() > 0) {
echo $result[0]->id;
}
SELECT * FROM table WHERE name like 'sample%'
code will be
$this->db->where_or("name","sample","after");//key are both , before , after
$result = $this->db->get("table");
if($this->db->count() > 0) {
echo $result[0]->id;
}
Another Example
SELECT * FROM table WHERE name like 'sample%' AND place like '%myplace'
code will be
$this->db->where_or("name","sample","after");
$this->db->where_or("place","my place","before"); $result = $this->db->get("table");
if($this->db->count() > 0) {
echo $result[0]->id;
}
SELECT * FROM table WHERE name like 'sample%' OR place like '%myplace'
code will be
$this->db->where_or("name","sample","after");
$this->db->where_or_like("place","my place","before"); $result = $this->db->get("table");
if($this->db->count() > 0) {
echo $result[0]->id;
}
SELECT * FROM table ORDER BY `name` ASC
code will be
$array['name']='ASC';
$this->db->order($array);
$result = $this->db->get("table");
if($this->db->count() > 0) {
echo $result[0]->id;
}
SELECT * FROM table LIMIT 5
code
$this->db->limit(5);
$result = $this->db->get("table");
if($this->db->count() > 0) {
echo $result[0]->id;
}
SELECT * FROM table LIMIT 5,10
$this->db->limit(10,5);
$result = $this->db->get("table");
if($this->db->count() > 0) {
echo $result[0]->id;
}
INSERT INTO table (name,place) VALUE ('sample','my place')
code:
$insert['name']='sample';
$insert['place']='my place';
$this->db->insert("table",$insert);
UPDATE table SET name = 'my place' WHERE id =1
code:
$this->db->where("id","1");
$update['name']='my place';
$this->db->update("table",$update);
DELETE FROM table where id=1
code:
$this->db->where("id","1");
$this->db->delete("table");
SELECT * FROM table INNER JOIN `other` ON `other`.id=`table`.other_id
code:
$this->db->join("other","`other`.id=`table`.other_id","INNER");
$this->db->get("table");
JOIN have 3 KEY , INNER,LEFT,*JOIN
$this->db->group_by("name");
$this->db->having("count(`name`)=1");
$this->db->count();
$sql = "SELECT * FROM table";
$result = $this->db->query($sql);
$this->db->select("id");
$this->db->where("name","sample");
$result = $this->db->get("table");
echo $this->db->sql;
$this->db->select("id");
$this->db->where("name","sample");
$result = $this->db->get("table");
var_dump($this->db->err);
//SELECT * FROM table WHERE a IS NULL
$this->db->where_null("a");
$this->db->get("table");
//SELECT * FROM table WHERE a IS NOT NULL
$this->db->where_null("a",false);
$this->db->get("table");
//SELECT * FROM table WHERE ( a = '1' AND b = '2' ) OR ( a = '5' AND b = '6' )
$this->db->bracket_open();
$this->db->where("a","1");
$this->db->where("b","2");
$this->db->bracket_close();
$this->db->bracket_open("OR");
$this->db->where("a","5");
$this->db->where("b","6");
$this->db->bracket_close();
$this->db->get("table");