CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New

How to use joins in codeIgniter


Normal Join

$this->db->select('*');
$this->db->from('articles');
$this->db->join('category', 'category.id = articles.id');
 
$query = $this->db->get();
 
/* Produces: */
/* SELECT * FROM articles JOIN category ON category.id = articles.id */

Join With Condition

$this->db->select('*');
$this->db->from('articles');
$this->db->join('category', 'category.id = articles.id');
$this->db->where(array('category.id' => 10));
 
$query = $this->db->get();
 
/* Produces: */
/* SELECT * FROM articles JOIN category ON category.id = articles.id where category.id = 10 */

Join with multiple tables

$this->db->select('*');
$this->db->from('articles');
$this->db->join('category', 'category.id = articles.id');
$this->db->join('comments', 'comments.id = articles.id');
 
$query = $this->db->get();
 
/* Produces: */
/* SELECT * FROM articles */
/* JOIN category ON category.id = articles.id */
/* JOIN comments ON comments.id = articles.id */

Join with specific type (left, right, outer, inner, left outer, and right outer)

$this->db->select('*');
$this->db->from('articles');
$this->db->join('category', 'category.id = articles.id','left');
$this->db->join('comments', 'comments.id = articles.id','left');
 
$query = $this->db->get();
 
/* Produces: */
/* SELECT * FROM articles */
/* LEFT JOIN category ON category.id = articles.id */
/* LEFT JOIN comments ON comments.id = articles.id */


Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in /www/wwwroot/studentstutorial.com/includes/get_article.php on line 13