Database reference

<?php
// select
$this->db->from('posts');
$query = $this->db->get(); // SQL: SELECT * FROM posts
 
// join
$this->db->from('posts');
$this->db->join('comments', 'comments.id = posts.id');
$query = $this->db->get(); // SQL: SELECT * FROM posts JOIN comments ON comments.id = posts.id
 
// where
$this->db->from('posts');
$this->db->where('title', $title);
$this->db->where('id', $id);
$query = $this->db->get(); // SQL: SELECT * FROM posts WHERE title = 'About' AND id = 40 
 
// where
$this->db->from('posts');
$this->db->where('title !=', $title);
$this->db->where('id <', $id);
$query = $this->db->get(); // SQL: SELECT * FROM posts WHERE title != 'About' AND id < 45
 
// or where
$this->db->from('posts');
$this->db->where('title !=', $title);
$this->db->or_where('id >', $id);
$query = $this->db->get(); // SQL: SELECT * FROM posts WHERE title != 'About' OR id > 50
 
// order by
$this->db->from('posts');
$this->db->order_by('id', 'DESC');
$query = $this->db->get(); // SQL: SELECT * FROM posts ORDER BY `id` DESC
 
// limit
$this->db->from('posts');
$this->db->limit(10);
$query = $this->db->get(); // SQL: SELECT * FROM posts LIMIT 10
 
 
// insert
$data = array(
	'id' => $id,
	'title' => $title
);
$this->db->insert('posts', $data); // SQL: INSERT INTO posts (id, title) VALUES (40, 'About')
 
 
// update
$data = array(
	'title' => $title,
	'content' => $content,
);
$this->db->where('id', $id);
$this->db->update('posts', $data); // SQL: UPDATE posts SET title = '{$title}', content = '{$content}' WHERE id = $id
 
 
// delete
$this->db->where('id', $id);
$this->db->delete('posts'); // SQL: DELETE FROM posts WHERE id = $id
 
 
// chaining:
$query = $this->db->select('title')
				->where('id', $id)
				->limit(10)
				->get('posts');
?>

Leave a Reply

Your email address will not be published. Required fields are marked *