Skip to content Skip to sidebar Skip to footer

How To Show Database In CodeIgniter

Select is an order query in database to display the data in database the. Order select can display data in one table or several table in one databases.

Read also:

In Codeigniter, writing select can be done in 2 ways, namely:

1. In the traditional way.

2. By the way active records.

Syntax query executed by query methods () on database classhere is the syntax query traditional:

1. With active record, programmer can manipulate data simply and scriptt to a minimum

2. Codeigniter men-generate syntax query based on method active record called by programmer.

3. Query which in generate ensure that it is supported by the DBMS used, for example mysql with a different oracle and Codeigniter can adjust it automatically.

4. The syntax that is usually used is as follows:

5. By using GET() active record will produce select to table specified in the parameter method get() the.

There are two types to retrieve results querythat is :

1. Result set type Object.

2. Result set type Array.

Well, now the author will provide a tutorial for displaying database queries to web pages using the CodeIgniter framework. Here are the steps to do it:

1. Enable Apache and MySQL on the XAMPP Web Server.

2. Open http://localhost/phpmyadmin/ then create a database with the name book.

3. Inside database book, then create a table with the name book with fields as follows:

4. After that, enter record into the table book as follows:

5. Change files autoload.php in the folder application/config.

$autoload['libraries'] = array('database');

Script above is used to call database libraries automatically.

$autoload['helper'] = array('url');

As for calling helper url can automatically add that code snippet.

6. Open the file routes.php in the folder application/config.

$route['default_controller'] = 'cbuku';$route['404_override'] = '';$route['translate_uri_dashes'] = FALSE;

Script it is used to make a name controller by default, so it will automatically appear a controller named cbuku.php contained in the folder controller.

7. Then open the file database.php in the folder config and change the database configuration.

$db['default'] = array(	'dsn'	=> '',	'hostname' => 'localhost',	'username' => 'root', //username mysql	'password' => '', //isi password jika ada	'database' => 'buku', //nama database yang telah dibuat

Fill in the database array with the database name that you created earlier.

8. If it is already made database and the configuration process has been formed, now is the time to create the file controller by the name cbuku.php

load->model('mbuku');}function index(){	$data['title']='ini contoh untuk menampilkan data';	$data['buku']=$this->mbuku->selectAll();	$this->load->view('vbuku',$data);	}}?>

9. Next, make model by the name mbuku.php and save it in the folder models.

db->order_by("Judul","desc"); 		return $this->db->get('buku')->result();   }}?>

10. To display the website, now is the time to create files in the folder views by the name vbuku.php.

			Tampil Buku	$title
"; echo "

"; echo ""; $no=1; foreach($buku as $b) { echo ""; $no++;} echo "
No Judul Pengarang Kategori
$no $b->Judul $b->Pengarang $b->Kategori
"; ?>

11. Open browser you and then type the URL http://localhost/ci/.

Well, that was an example program to display data from database use framework Codeigniter. With the Codeigniter concept, namely MVC (Models, Views, Controllers) it will make it easier for developers to create dynamic websites.