how to fetch data from database in codeigniter?

Today, We want to share with you how to fetch data from database in codeigniter.In this post we will show you insert and display data from database in codeigniter, hear for select query in codeigniter we will give you demo and example for implement.In this post, we will learn about codeigniter count rows in view & controller with an example.

Retrieve data from database using CodeIgniter framework

In this example we will learn all about how to fetch a record or data from MySQL database using CodeIgniter framework PHP.

Sql Table

CREATE TABLE students (
  `id` int(11) NOT NULL,
  `profile_nm` varchar(30) NOT NULL,
  `sir_nm` varchar(30) NOT NULL,
  `email` varchar(30) NOT NULL,
   PRIMARY KEY (id)
);

Models

Student.php (Path: application\models\Student_model.php)

load->database();
	

	$this->load->model('Student_model');
	}

	public function displaydata()
	{
		$result['data']=$this->Student_model->display_records();
		$this->load->view('display_records',$result);
	}
	
}
?>

Get codeIgniter Data

get all data from db in codeigniter using student_fetch.php

db->query("select * from students");
	return $query->result();
	}
	
} 

Display records

here simple you can display data with codeigniter view file like as a display_records.php (Path: application\views\insert.php)



Display records - www.pakainfo.com

 

";
  echo "";
  echo "";
  echo "";
  echo "";
  echo "";
  $i++;
  }
   ?>
Sr No First_name Last_name Email Id Delete Update
".$i."".$student->profile_nm."".$student->sir_nm."".$student->email."

and last step Now run the example on your browser like chrome with the below URL:

http://your-domainname/codeIgniter/index.php/Student/displaydata

After here you can simply retrive the students data the table look like this.

Id Profile name Sir name Email Id
1 Virat Kohali [email protected]
2 Bhavika Pethani [email protected]
3 Sejal Ramani [email protected]

I hope you get an idea about how to fetch data from database in codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment