Yii Framework2.0 development tutorial (3) getting started with mysql, yiiframework2.0

Source: Internet
Author: User

Yii Framework2.0 development tutorial (3) getting started with mysql, yiiframework2.0

Use the code of tutorial (2)


Step 1: Create the database zhyoulun in the local mysql database

Step 2: Create a table in the database and insert several data entries

CREATE TABLE `country` (  `code` CHAR(2) NOT NULL PRIMARY KEY,  `name` CHAR(52) NOT NULL,  `population` INT(11) NOT 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 ('CA','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','United Kingdom',59623400);INSERT INTO `country` VALUES ('IN','India',1013662000);INSERT INTO `country` VALUES ('RU','Russia',146934000);INSERT INTO `country` VALUES ('US','United States',278357000);


Step 3: configure the database connection for yii. The configuration file is config/db. php.

<? Php // The configured database Connection can be accessed in the application through Yii: $ app-> db return ['class' => 'yii \ db \ connection ', 'dsn '=> 'mysql: host = localhost; dbname = zhyoulun', 'username' => 'root', 'Password' => '123 ', 'charset' => 'utf8',];


Step 4: Add code in controllers/ZhyoulunController. php

public function actionCountry(){return $this->render('country');}


Step 5: Create Country. php In the models folder

<? Php namespace app \ models; use yii \ db \ ActiveRecord; // you do not need to write any code in it. Just as now, Yii can guess the corresponding data table name based on the class name. Class Country extends ActiveRecord {}


Step 6: Create country. php In the views/zhyoulun/folder

<? Phpuse app \ models \ Country; // obtain all rows in the country table and sort them 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> ';

Step 7: Visit the URL http: // localhost/basic/web/index. php? R = zhyoulun/country



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


Reprinted please indicate the source: http://blog.csdn.net/zhyoulun/article/details/40454583


I bought a free U. S. Record-free full space with a mysql database. I want to install discuz x31 and configure the database

First, you must log on to the website control panel of the U.S. space backend. Since there is a mysql database, I guess it should also provide the PHPMyAdmin function. Through PHPMyAdmin, you must first create a mysql database, you must enter the mysql database name, username, and password when creating the database ). after creating the mysql database, you can start to install discuz.

First, upload discuz x3.1 to the US space, and then install the Discuz according to the discuz installation tutorial. During discuz installation, enter the mysql database information (that is, the information recorded when the mysql database was created ), finally, install discuz.

How to run yii framework-PHP framework development

This mechanism is nothing more than two points: 1. DRY2 and autoload.
 

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.