Database connectivity in CodeIgniter with example and code
Technology 0 CommentsThe first creates the database in Mysql and another database .
File path and folder path is database.php : application\config.php
Database.php
‘hostname’ => ‘localhost’,
‘username’ => ‘root’, write username
‘password’ => ”, write password
‘database’ => ‘codignater’, write Database
‘dbdriver’ => ‘mysqli’,
Crate a users.php file in controller folder and create controller
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class Users extends CI_Controller { // home show
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* – or –
* http://example.com/index.php/welcome/index
* – or –
* Since this controller is set as the default controller in
* config/routes.php, it’s displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index() // funstion hit name access
{
$this->load->model(‘usermodel’); // file name in model usermodel.php file name
$data[‘users’] = $this->usermodel->getuser(); // mean that data pic in model and send in users variable
$this->load->view(‘userlist’,$data); // view userlist.php file name
//$this->about();
}
}
?>
Crate a usermodel.php file in models folder and create models
usermodel.php
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class Usermodel extends CI_Controller { // Aome show
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* – or –
* http://example.com/index.php/welcome/index
* – or –
* Since this controller is set as the default controller in
* config/routes.php, it’s displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function getuser() // funstion hit name access
{
$this->load->database(); // inbuild function database load
$q = $this->db->query(“SELECT * FROM userid”);
//print_($q); //check queary
return $q->result_array(); //inbuld function
//return $q->result();
// $result = $q->result_array();
// print_r($result);
}
}
?>
Crate a userlist.php file in models folder
userlist.php file
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
?><!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>index to CodeIgniter</title>
<style type=”text/css”>
::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
#body {
margin: 0 15px 0 15px;
}
#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<div id=”container”>
<?php foreach($users as $user) { ?>
<?php echo $user[‘id’]; ?>
<?php echo $user[‘name’]; ?> <br>
<?php }
?>
<?php //foreach($users as $user); { ?>
<div>
<div><?php //echo $user[‘id’]; ?></div><br>
<div><?php //echo $user[‘name’]; ?></div><br>
</div>
<?php // } ?>
<h1>Welcome to CodeIgniter!</h1><a href=”about”>about</a>
</div>
</body>
</html>
Buy online Rajasthan gk book Railway JE CBT REET PAtwari Book SSC CGL Clerk GD Book Buy online Rajasthan gk book Railway JE CBT REET PAtwari Book SSC CGL Clerk GD Book