Database connectivity in CodeIgniter with example and code

PHP 0 Comments

The 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>

 

 


Bank PO clerk, SSC, Railway (RRB), IBPS, UPSC, IAS, RAS, SBI, 1st 2nd 3rd grade teacher,REET, TET, Rajasthan police SI, Delhi police, के महत्वपूर्ण सवाल जवाब के लिए रजिस्टर करे
आप करेंट अफेयर्स, Job, सामन्य ज्ञान ओर सभी exams संबंधित Study Material की जानकारी हैतु इस पेज को Like and Visit करे
https://www.facebook.com/myshort.Trick
and WWW.myshort.in

share..Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0

Leave a Reply

Your email address will not be published. Required fields are marked *


*