Complete implementation method of database data retrieval example for CI framework, ci framework
The example in this article describes the complete implementation method of database data retrieval as an entry-level example of CI framework. It is written for beginners. This is the simplest example that can be adjusted. Share it with everyone for your reference. The specific implementation method is as follows:
1. Download CI framework
2. Configuration
database.php configuration:
Set connection parameters for the database server:
Copy code The 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 a table
Copy code The 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 ;
Just fill in a few pieces of data yourself
4. Implement MVC
1) Implement M--get data
Create a new file mtest.php
under CI’s models
Copy code The code is as follows:
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 characters
$query = $this->db->get('users', 10);
return $query->result();
}
}
?>
Description:
parent::__construct(); essential
$this->load->database(); must be indispensable otherwise an error will be reported
It is also possible to implement an "auto-connect" function, which will automatically instantiate the database class every time a page is loaded. To enable "auto-connection", add database to the library array in the following file:
application/config/autoload.php
Otherwise it would be written on every page like here.
You can also use
to copy the code . The code is as follows:
$query = $this->db->query('select * from users') ;
Write your own SQL like this
2) Implement C--Determine which data to retrieve
Create a new file test.php
under CI controllers
Copy code The code is as follows:
class Test extends CI_Controller {
function Test(){
parent::__construct();
}
function index(){
$this->load->helper('form');
$data['title'] = "Homepage";
$data['headline'] = "Enter user information";
//Multidimensional 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 code The code is as follows:
$this->load->model('mtest');
Load the model into the array:
Copy code The code is as follows:
$data['query1'] = $this->mtest-> get_last_ten_entries();
Reprint the array to the page:
Copy the code The code is as follows:
$this->load->view('users',$data );
2) Implement V--page display
Create a new file user.php
under CI’s views
Copy code The code is as follows:
echo $title;?>
echo count($query1);
foreach ($query1 as $v1) {
foreach ($v1 as $v2) {
echo "$v2n";
}
}
for ($row=0;$row
echo $query1[$row]->name."";
}
?>
- name;?>