CodeIgniter – Working with Database
Hello Readers,
I would like to write about database connection in CodeIgniter and how to insert and retrieve values from database.
First step is to create a database and to add a table in it. Open browser and enter http://localhost/phpmyadmin (Assuming you use xampp/wamp). Click “Databases” in the menu. Create a database and a table using the following steps.
- Enter the name of the database in the textbox.(ex. sample_db)
- Press Create button
- Now database will be created and will be shown in the left sidebar
- Click on the database you have just created. (sample_db)
- Enter the name of the table (table1) and number of columns required (4)
- Enter the column names and their datatype. id(int),name(varchar),age(int),city(varchar)
- For the column id, check the field AI, located next to index filed
- Now press Save.
We have created databas and added table into it.
The next step is to configure the database settings into the CodeIgniter. To do that explore to xampp/exercise/application/config/database.php and change the following values.
$active_group = 'default'; $active_record = TRUE; $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; // add username of the database $db['default']['password'] = ''; // password for the db. $db['default']['database'] = 'sample_db'; // database name $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE;
Now save the file and open application/controller/welcome.php, In index() function replace the following code
public function index() { $this->load->model('common_model'); $data = array('id'=>'','name'=>'Robert','age'=>15,'city'=>'Sydney'); $id = $this->common_model->add_data($data); echo "Values Inserted Successfully. ID is".$id; } public function get(){ $this->load->model('common_model'); $value = $this->common_model->get_data(); print_r($value); }
In application/models/ create a new file ans save it as common_model.php and add the following code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Common_model extends CI_Model { public function add_data($r_data){ $this->load->database(); $this->db->insert("table1",$r_data,); return $this->db->insert_id(); } public function get_data(){ $this->load->database(); $get = $this->db->get("table1"); return $get->result_array(); } } ?>
To view the output, open browser and enter http://localhost/exercise. Now values will be inserted. To view the inserted values, open http://localhost/exercise/index.php/get
If you have any clarifications, add a comment and I will reply.
Thanks for reading.
Related Posts
Nagarajan
View Nagarajan's Profile
Latest posts by Nagarajan (see all)
- Random Image viewer using AngularJS - June 30, 2016
- Building your first AngularJS app – Shopping list - June 22, 2016
- How to use Cookies in AngularJS - October 20, 2015
in welcome.php file, on line number 2, Parse error: syntax error, unexpected ‘public’ (T_PUBLIC) in C:\xampp\htdocs\ci\application\controllers\welcome.php on line 2 it is showing this .. plz help ,, i am new to codeign…
Hello Shubhesh,
Can you explain in detail. Can you provide your full code so that I get clear idea about the error.
Thanks
HI, I FOLLOWED INSTRUCTIONS AS DIRECTED AND PROVIDED IS THE ERROR I GOT.
Parse error: syntax error, unexpected ‘)’ in C:\xampp\htdocs\code\application\models\common_model.php on line 5
load->database();
$this->db->insert(“user”,$r_data,);
return $this->db->insert_id();
}
public function get_data(){
$this->load->database();
$get = $this->db->get(“user”);
return $get->result_array();
}
}
?>
Can you give me the full code of common_model.php? what is in line no 5?
$this->db->insert(“table1”,$r_data,);
i follow complete steps but at last i got
“Unable to connect to your database server using the provided settings.
Filename: C:\xampp1\htdocs\code\system\database\DB_driver.php
Line Number: 127”
this error
No direct script access allowed
this message is coming when we try to run the sctript
followed u r steps and got this error
Parse error: syntax error, unexpected ‘$data1’ (T_VARIABLE) in C:\xampp\htdocs\exercise\application\controllers\welcome.php on line 24
This is my code, plz reply me
public function index()
{
$this->load->model(‘common_model’);
4
$data1 = array(‘id’=>”,’name’=>’Robert’,’age’=>15,’city’=>’Sydney’);
5
$id = $this->common_model->add_data($data1);
6
echo “Values Inserted Successfully. ID is”.$id;
7
}
8
public function get(){
9
$this->load->model(‘common_model’);
10
$value = $this->common_model->get_data();
11
print_r($value);
12
}
}
@OLA ,
At line 5 Just need to remove the extra comma, $this->db->insert(“user”,$r_data);
Hope this will work fine for you.
http://localhost/exercise/index.php/get – When I tried view the output with this I shows an error message saying 404 page not found …