Codeigniter Database Quick Start: Example Code
Codeigniter Database Quick Start: Example Code – This database reference is used for showing how the database class is used. We can also use this reference to fetching the data from database and display on the browser with multiple and single result of query. We can load datbase like this $this->load->database(‘database_name’);. If you already loaded database in autoload or configure in file config/database.php then no need to load manually. Here in this tutorial, we are going to explain how to use quick start database reference.
Codeigniter Database Quick Start | : Example Code.
Let us understand how database quick start reference works in codeigniter with examples.
Load datbase quick start reference
First load database quick start reference to use its functions, this reference can be loaded simply as below-
How to load database quick start reference:
$this->load->database('database_name'); |
Functions:-
There are following functions available in database quick start reference. Now we will explain one by one.
- 1. Standard Query With Multiple Results.
- 2. Standard Query With Single Result.
- 3. Standard Insert.
- 4. Query Builder Insert.
- 5. Query Builder query.
1. Standard Query With Multiple Results.
Here is simple demo of standard query with multiple results.
Syntax of standard query with multiple results.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class quick_start_controller extends CI_Controller { public function startDatabase() { $query = $this->db->query('SELECT name, username, email FROM admin'); foreach ($query->result() as $row) { echo $row->name;echo "<br>"; echo $row->username;echo "<br>"; echo $row->email;echo "<br>"; } echo 'Total Results: ' . $query->num_rows(); } } ?> |
Output will be like this:-
2. Standard query with single results.
Here is simple demo of standard query with single results.
Syntax of standard query with single results.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class quick_start_controller extends CI_Controller { public function singleResult() { $query = $this->db->query('SELECT name FROM admin'); $row = $query->row(); echo $row->name; } } ?> |
Output will be like this:-
3. Standard Insert.
Here is simple demo of standard insert data in database.
Syntax of standard insert data in database.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class quick_start_controller extends CI_Controller { public function standardinsert() { $sql = "INSERT INTO mytable (title, name) VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")"; $this->db->query($sql); echo $this->db->affected_rows(); } } ?> |
Output will be like this:-
4. Query Builder Insert.
Here is simple demo of query builder insert.
Syntax of query builder insert.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class quick_start_controller extends CI_Controller { public function quickInsert() { $data = array('title' => "sonu",'name' => "kumar"); $sql = $this->db->insert('blog', $data); if($sql) { echo "Inserted successfully"; } else { echo "unable to insert"; } } } ?> |
Output will be like this:-
5. Query Builder query.
Here is simple demo of query builder query.
Syntax of query builder query.
defined('BASEPATH') OR exit('No direct script access allowed'); class quick_start_controller extends CI_Controller { public function insert() { $query = $this->db->get('blog'); foreach ($query->result() as $row) { echo $row->title; } } } ?> |
Output will be like this:-
Advertisements