CI framework Entry Example-complete implementation of database data retrieval _ php instance

Source: Internet
Author: User
Tags php foreach
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.