Yii Framework2.0 Development Tutorial (3) database MySQL Getting Started

Source: Internet
Author: User
Tags yii

The code that follows the tutorial (2)


The first step is to create a new database in the local MySQL database Zhyoulun

Step two, create a new table in the database and insert several data

CREATE TABLE ' country ' (  ' Code ' char (2) NOT null PRIMARY KEY,  ' name ' char (*) NOT NULL,  ' population ' INT (one) N OT NULL default ' 0 ') engine=innodb default Charset=utf8; INSERT INTO ' country ' values (' AU ', ' Australia ', 18886000); insert INTO ' country ' values (' BR ', ' Brazil ', 170115000); insert Into ' country ' values (' cas ', ' Canada ', 1147000); insert INTO ' country ' values (' CN ', ' China ', 1277558000); INSERT INTO ' Country ' VALUES (' DE ', ' Germany ', 82164700); insert INTO ' country ' values (' FR ', ' France ', 59225700); insert INTO ' country ' VALUES (' GB ', ' Manchester Kingdom ', 59623400); insert INTO ' country ' values (' in ', ' India ', 1013662000); insert INTO ' country ' VALUES (' RU ', ' Russia ', 146934000); INSERT into ' country ' values (' US ', ' Manchester states ', 278357000);


The third step, the database connection configuration for Yii, configuration file is config/db.php

<?php//configured database connection can be used in the application through Yii:: $app->db visit return [    ' class ' = ' yii\db\connection ',    ' dsn ' = ' MySQL: Host=localhost;dbname=zhyoulun ',    ' username ' = ' root ',    ' password ' = ' 20092565 ',    ' charset ' and ' = ' UTF8 ',];


Fourth step, add code in controllers/zhyouluncontroller.php

Public Function Actioncountry () {return $this->render (' Country ');}


Fifth step, create a new country.php in the models directory

<?

PHP namespace App\models; Use yii\db\activerecord;//you don't have to write whatever code in it.

It just needs to be like today. Yii can be based on the class name to test the corresponding data table name. Class Country extends activerecord{}



Sixth step, create a new country.php in the views/zhyoulun/directory

<?phpuse app\models\country;//get all rows of country table and sort by name $countries = Country::find ()->orderby (' name ')->all () Echo ' <table border= "1" > "for ($i =0; $i <count ($countries); $i + +) {echo ' <tr> '; Echo ' <td> '. $ countries[$i [' Code ']. ' </td> '; Echo ' <td> '. $countries [$i] [' name ']. ' </td> '; Echo ' <td> '. $countries [$i] [' population ']. ' </td> '; Echo ' </tr> ';} Echo ' </table> ';

Seventh step, visit the URL http://localhost/basic/web/index.php?

R=zhyoulun/country

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvemh5b3vsdw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


References: http://www.yiichina.com/guide/2/start-databases


Reprint Please specify source: http://blog.csdn.net/zhyoulun/article/details/40454583

Yii Framework2.0 Development Tutorial (3) database MySQL Getting Started

Related Article

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.