SoFunction
Updated on 2025-03-10

CI framework introduction example: complete implementation method for database data acquisition

This article describes the complete implementation method of database data acquisition in the CI framework. It is written for beginners to read, and this is the easiest example that can be adjusted. Share it for your reference. The specific implementation method is as follows:

1. Download the CI framework

2. Configuration

Configuration:

Set connection parameters for the database server:

Copy the codeThe code is as follows:
$db['default']['hostname'] = "your-db-host"; 
$db['default']['username'] = "your-username"; 
$db['default']['password'] = "your-password"; 
$db['default']['database'] = "your-db-name"; 
$db['default']['dbdriver'] = "mysql";

3. Create tables

Copy the codeThe code is as follows:
CREATE TABLE IF NOT EXISTS `users` ( 
  `id` INT(8) NOT NULL AUTO_INCREMENT, 
  `name` VARCHAR(30) CHARACTER SET utf8 DEFAULT NULL, 
  `age` VARCHAR(3) CHARACTER SET utf8 DEFAULT NULL, 
  `sex` VARCHAR(2) CHARACTER SET utf8 DEFAULT NULL, 
  PRIMARY KEY  (`id`) 
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_estonian_ci AUTO_INCREMENT=14 ;

Fill in some data for yourself

4. Implement MVC
1) Implement M-get data
Create a new file under CI models

Copy the codeThe code is as follows:
<?php 
class Mtest extends CI_Model{ 
    function Mtest(){ 
        parent::__construct(); 
    } 
        function get_last_ten_entries() 
    {        
        $this->load->database(); 
mysql_query("SET NAMES GBK"); //Prevent Chinese garbled
        $query = $this->db->get('users', 10); 
        return $query->result(); 
    } 

?>

illustrate:

parent::__construct(); is indispensable
$this->load->database(); must not be missing or there will be an error
The "automatic connection" function can also be implemented, and the database class will be automatically instantiated when each page is loaded. To enable "auto-connect", add database to the library array in the following file:
application/config/
Otherwise, it will be written on each page like here.
Can also be used

Copy the codeThe code is as follows:
$query = $this->db->query('select * from users');

Write to your own SQL like this
 
2) Implement C--decide what data to get
Create a new file under CI controllers
Copy the codeThe code is as follows:
<?php 
class Test extends CI_Controller { 
  function Test(){ 
    parent::__construct(); 
  } 
  function index(){ 
    $this->load->helper('form'); 
$data['title'] = "Home";
$data['headline'] = "Input user information";
//Multi-dimensional array
    $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands'); 
    //$this->load->vars($data); 
    $this->load->model('mtest'); 
    $data['query1'] = $this->mtest->get_last_ten_entries(); 
    $this->load->view('users',$data); 
    //$this->load->view('newfile'); 
    //$this->load->view('a/newfile'); 


?>

Call model:
Copy the codeThe code is as follows:
$this->load->model('mtest');

Load the model into the array:
Copy the codeThe code is as follows:
$data['query1'] = $this->mtest->get_last_ten_entries();

Repost the array to the page:
Copy the codeThe code is as follows:
$this->load->view('users',$data);

2) Implement V--page display
Create a new file under CI's views

Copy the codeThe code is as follows:
<head> 
<title><? echo $title;?></title> 
</head> 
<body> 
<ul> 
<?php foreach($todo_list as $item):?> 
<li><?php echo $item;?></li> 
<?php endforeach;?> 
</ul> 
<ul> 
<? echo count($query1); 
foreach ($query1 as $v1) { 
    foreach ($v1 as $v2) { 
        echo "$v2\n"; 
    } 

for ($row=0;$row<count($query1);$row++) { 
    echo $query1[$row]->name."</br>"; 

?> 
 
<?php foreach($query1 as $v):?> 
<li><?php echo $v->name;?></li> 
<?php endforeach;?> 
</ul> 
</h2><?php echo $headline; ?></h2> 
</body> 
</html>

Note: You can use For and Foreach to find out the data you want!
Note: If the entire page is garbled, the head of the web page is probably like this.
Copy the codeThe code is as follows:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

If you are not using CI to connect to the database, add the following code to the database connection section.
Copy the codeThe code is as follows:
mysql_query("SET NAMES GBK"); //Prevent Chinese garbled
mysql_query("set names utf8;");//Add after mysql_select_db("");.
//Prevent Chinese garbled codes and you need to look at your database character set

Files under CI config
Copy the codeThe code is as follows:
$db['default']['char_set'] = 'utf8';  //utf8.  The database character set is also utf8
$db['default']['dbcollat'] = 'utf8_general_ci';

I hope this article will be helpful to everyone's learning of CI framework programming.