Monday, June 20, 2016

Database connect and retrieve data

Database connect and retrieve data.

First we have to change data in  autoload.php in config folder as follows.
$autoload['libraries'] = array('database');
Next change database.php file data as follows.
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydb',
Here host is localhost ,user is root user, default password for user is no password,I creat database as mydb.
In mydb database there is a table name users with three attributes id,fname,email.

We retrieve data according to following steps.
Create  Model.php file in model folder
<?php
class User_model extends CI_Model{

                public function getFirstName(){
                                $query=$this->db->query("SELECT fname FROM users");
                                return $query->result();
                }
}
?>
Then create Message.php in views folder
<!DOCTYPE html>
<html>
<head>
                <title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>

Now create Hello2.php in controller
<?php
class Hello2 extends CI_Controller{

                public function index()
                {

                                $this->home();
                }

                public function home()
                {
                                $this-> load->model('user_model');
                                $data['names']=$this->user_model->getFirstName();
                                $this->load->view('message',$data);
                }
}

?>
Now edit the welcome.php in controller
$this->load->view('welcome_message');
To
$this->load->view('message');

I already  insert data to mydb database users table.
We can retrieve data by using following url
output will be
Here database details included sql file.



-- phpMyAdmin SQL Dump
-- version 4.3.11
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Jun 20, 2016 at 06:34 PM
-- Server version: 5.6.24
-- PHP Version: 5.6.8

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `mydb`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(50) NOT NULL,
  `fname` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `fname`, `email`) VALUES
(1, 'nalaka', 'nalaka@gmail.com'),
(2, 'sarath', 'sarath@gmail.com');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(50) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

No comments:

Post a Comment