The simplest example of getting started with the CI framework-database fetching data

Source: Internet
Author: User
Tags php foreach

This is for beginners to see, this is the simplest can be adjusted to the example, many online examples of beginners in fact the local run, lack of this less.

1. Download the CI framework (find it yourself)

2. Configuration

database.php configuration:
To set the connection parameter for the database server:

$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. Build a table

CREATE TABLE IF not EXISTS ' users ' (  ' IDs ' INT (8) Not NULL auto_increment,  ' name ' VARCHAR () CHARACTER SET UTF8 DE FAULT 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 a few data yourself

4. Implementing MVC

1) Implement m--data collection

Create a new file under CI models mtest.php

<?phpclass 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 ', ten);        return $query->result ();    }    }? >

Description

Parent::__construct (); no less
$this->load->database (); must not be less or will be an error

You can also implement the Auto Connect feature, which will automatically instantiate the database class each time a page is loaded. To enable automatic connection, you can add it in the library array in the following file database :

application/config/autoload.php

不然就要像这里一样写在每个页面上。

You can also use $query = $this->db->query (' SELECT * from users ');

Write your own SQL in this way

2) Implement c--decision to take those data

Create a new file under CI controllers test.php

<?phpclass Test extends Ci_controller {   function Test () {    parent::__construct ();  }   function index () {    $this->load->helper (' form ');    $data [' title '] = "Home";    $data [' headline '] = "input 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: $this->load->model (' mtest ');

Load the model into the array: $data [' query1 '] = $this->mtest->get_last_ten_entries ();

The array is reproduced on the page: $this->load->view (' users ', $data);



2) Implement v--page display

CI Views under Create a new file user.php

Description: You can use for and foreach multiple methods to find the data you want!

Description: If the whole page is garbled , the page header is probably the case.

<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.

mysql_query ("SET NAMES GBK"); Prevent Chinese garbled characters
mysql_query ("set names UTF8;");  //In mysql_select_db ("");

Prevent Chinese garbled to see your database character set

database.php file under CI Config

$db [' Default '] [' char_set '] = ' utf8 ';  UTF8.  The database character set is also utf8$db[' default ' [' dbcollat '] = ' utf8_general_ci ';

REF:

http://blog.csdn.net/21aspnet/article/details/6599780

The simplest example of getting started with the CI framework-database fetching data

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.