Yii Framework, addition, deletion, modification, and query, SQL

Source: Internet
Author: User
Yii Framework, addition, deletion, modification, query, and addition, deletion, modification, and query of SQL SQL Yii data

1. $ SQL = "insert into tbl_user ('id', 'username', 'password') values (null, 'alu', '123 ');
2. $ SQL = "select * from tbl_user where id = 1;
3. $ SQL = "update tbl_user set password =" 123456 "where id = 6 ";
4. $ SQL = "delete from tbl_user where id = 6 ;";
// Set the character encoding
Mysql_query ("set names utf8 ");
$ Result = mysql_query ($ SQL) or die ('SQL statement error, the system prompts:'. mysql_error ());

5. $ num = mysql_num_rows ($ result) and $ num = mysql_fetch_array ($ result );
How should I write the above statements in the yii Framework? [/Color] [/color]


Reply to discussion (solution)

Utf8 encoding is written to the configuration file main. php:
'DB' => array (
'Connectionstring' => 'MySQL: host = localhost; dbname = test ',
'Default' => true,
'Username' => 'root ',
'Password' => '123 ',
'Charset' => 'utf8', // encoding is set here
'Tableprefix' => 'tbl _',
),
Controller:
$ Conn = Yii: app ()-> db;
$ Command = $ conn-> createCommand ();
Directly write the statement:
$ Command-> text = 'select * from {user} where id = 1 ';
$ DataReader = $ command-> query ();
Read data:
Foreach ($ dataReader as $ row)
{Echo $ row ['name'];}

The INSERT, UPDATE, and DELETE statements are similar to the preceding statements, but execute:
$ RowCount = $ command-> execute (); the number of affected rows is returned;

One of the major functions of Yii FrameWork is ActiveRecord. ActiveRecord is convenient and error-prone without the need to use SQL.

@ Dahuzizyd: how should we add, delete, modify, and query data? data must be stored in the database!

ActiveRecord of yii is very powerful. let's look at the class library manual.

It's nice to use gii to automatically generate it.

Http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.arr
Chinese manual on yii official website!
After you read it, you can do it on the fly!

Take a look at ActiveRecord and Model of Yii. Yii has Related methods for database operations. We recommend that you check the manual. Recommended website http://www.yiiframework.com/doc/guide/1.1/zh_cn

Recommended to see http://www.yiibook.com, the third seems to have translated
If you are familiar with using Yii, you can use DB Migration for database design and basic data storage.
Or, after creating the table, use Gii to generate the corresponding CRUD.

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.