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;?>
Note: You can use For and Foreach methods to find the data you want!
Note: If the entire page is garbled, the header of the web page will probably look like this.
Copy code The code is as follows:
If you are not using CI to connect to the database, add the following code in the database connection section.
Copy code The code is as follows:
mysql_query("SET NAMES GBK"); //Prevent Chinese garbled characters
mysql_query("set names utf8;");//Add after mysql_select_db("");.
//To prevent Chinese garbled characters, it depends on your database character set
database.php file under CI config
Copy code The code is as follows:
$db['default']['char_set'] = 'utf8'; //utf8. Database character set Also utf8
$db['default']['dbcollat'] = 'utf8_general_ci';
I hope this article will be helpful to everyone’s learning of CI framework programming.
$this->input->post(); Get the form data
Then $this->db->insert(table name, $data); insert into the database
Reconfigure database settings $this->db->databse($config);
$config is an array containing
$config['host'] = ''
$config ['database'] = ''
For configuration, please refer to the configuration file database.php
in CI
http://www.bkjia.com/PHPjc/906671.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/906671.htmlTechArticleComplete implementation method of database retrieval example for CI framework Get the complete implementation method of data. It is written for beginners, this...