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.