This article mainly introduces the complete implementation method of database fetching data in the CI framework introductory example, including the complete process of configuration, table creation and implementation of MVC. Friends in need can refer to the following examples in this article, which describes the complete database fetching data in the CI framework introductory example Implementation. 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 the CI framework
2. Configuration
Database. php Configuration:
Set the connection parameter for the database server:
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
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;
Enter a few pieces of data by yourself
4. Implement MVC
1) Implement M -- get data
Create a file mtest. php under models of CI
The 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"); // prevents Chinese garbled characters
$ Query = $ this-> db-> get ('users', 10 );
Return $ query-> result ();
}
}
?>
Note:
Parent: :__ construct (); indispensable
$ This-> load-> database (); it must be rare. Otherwise, an error will be reported.
You can also enable the "Automatic Connection" function to automatically instantiate the database class when each page is loaded. To enable automatic connection, you can add a database to the library array in the following file:
Application/config/autoload. php
Otherwise, it will be written on every page like this.
You can also use
The Code is as follows:
$ Query = $ this-> db-> query ('select * from users ');
Write your own SQL
2) Implement C -- determine the data to be retrieved
Create a file test. php under the controllers of CI.
The Code is as follows:
<? Php
Class Test extends CI_Controller {
Function Test (){
Parent: :__ construct ();
}
Function index (){
$ This-> load-> helper ('form ');
$ Data ['title'] = "Homepage ";
$ Data ['headline'] = "entering user information ";
// Multi-dimensional array
$ Data ['do _ list'] = array ('clean house', 'Call Ms', '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:
The Code is as follows:
$ This-> load-> model ('mtest ');
Load the model into the array:
The Code is as follows:
$ Data ['query1'] = $ this-> mtest-> get_last_ten_entries ();
Reprint the array to the page:
The Code is as follows:
$ This-> load-> view ('users', $ data );
2) Implement V-page display
Create a file user. php under views of CI
The Code is as follows:
<? Echo $ title;?>
<? Php foreach ($ todo_list as $ item):?>
- <? Php echo $ item;?>
<? Php endforeach;?>
<? Echo count ($ query1 );
Foreach ($ query1 as $ v1 ){
Foreach ($ v1 as $ v2 ){
Echo "$ v2 \ n ";
}
}
For ($ row = 0; $ row Echo $ query1 [$ row]-> name ."
";
}
?>
<? Php foreach ($ query1 as $ v):?>
- <? Php echo $ v-> name;?>
<? Php endforeach;?>
<? Php echo $ headline;?>
Note: You can use the For and Foreach methods to find the data you want!
Note: If the whole page is garbled, the webpage header is like this.
The Code is as follows:
If you do not use CI to connect to the database, add the following code to the database connection section.
The Code is as follows:
Mysql_query ("set names gbk"); // prevents Chinese garbled characters
Mysql_query ("set names utf8;"); // After mysql_select_db.
// Prevent Chinese garbled characters from viewing your database Character Set
Database. php file under CI config
The 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 help you with CI framework programming.