How to use MySql with CodeIgniter

Hi guys! In this tutorial I will explain to you how you can extract and display data from database in your CodeIgniter project.

I have created a new table in my database, called data with two columns: id and title.

Now I will create a new model, called data with this content:

<?php
class Data extends CI_Model {
public function __construct()
{
$this->load->database();
}
function get_data() {
$query = $this->db->query('SELECT * FROM data'); // a mysql query
return $query->row_array();
}
}
?>

Then I will connect the created model with my Controller:

public function __construct()
{
parent::__construct();
$this->load->model('data');
}

Next, I will add this function:

function index() {
$data["title"] = $this->data->get_data();
$this->load->view('index',$data);

}

And, in my view index.php file I will add this content:

<?php
echo $title["title"];
?>

Video tutorial:









Leave a Comment