Yii, SQL, add, delete, modify, and query [color = # 0000FF] 1. $ SQL = & nbsp; "insert & nbsp; into & nbsp; tbl_user ('id', 'username', 'password') values (null, 'alu ', '123'); 2. $ SQL = & nbsp; "yii, SQL, add, delete, modify, and query
[Color = # 0000FF] 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]
------ Solution --------------------
It seems that I have returned you a post, and I will return it again:
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 --------------------
In fact, it is easier to use ActiveRecord for simple CRUD operations?
$ Model = User: model ()-> findByPk (1 );
$ Model-> password = 'new password ';
$ Model-> save ();
------ Solution --------------------
Yii built-in method
------ Solution --------------------
Reference:
In fact, it is easier to use ActiveRecord for simple CRUD operations?
$ Model = User: model ()-> findByPk (1 );
$ Model-> password = 'new password ';
$ Model-> save ();
In addition, CRUD can be automatically generated. it is convenient to use yiic shell or gii on the command line interface. first, model and then crud.