Yii Framework, add, delete, modify, and query, sql1. $ SQL & nbsp; insert & nbsp; into & nbsp; tbl_user ('id', 'username', 'password') values (null, alibaba Cloud, 123); 2. $ SQL & nbsp; select & nbsp; * & nbsp; from & nbsp; tbl_user & nbsp; where & nbsp yii Framework, add, delete, modify, and query, SQL
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]
------ 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;
------ Solution --------------------
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!
------ Solution --------------------
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
------ Solution --------------------
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.