Yii, SQL, add, delete, modify, and query

Source: Internet
Author: User
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.

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.