Yii, SQL, additions and deletions to find out how to solve

Source: Internet
Author: User
Yii, SQL, delete and modify
[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 character encoding
mysql_query ("Set names UTF8");
$result =mysql_query ($sql) or Die (' SQL statement error, the system gives the hint is: '. mysql_error ());

5. $num = Mysql_num_rows ($result) and $num=mysql_fetch_array ($result);
How should the above statement be written in the YII framework?
[/color]
------Solution--------------------
It seems that you have a post, back again:

The UTF8 encoding is written to the configuration file main.php:
' DB ' =>array (
' connectionString ' = ' mysql:host=localhost;dbname=test ',
' Emulateprepare ' = true,
' Username ' = ' root ',
' Password ' = ' 123 ',
' CharSet ' = ' utf8 ', //Set code here
' Tableprefix ' = ' tbl_ ',
),
In the controller:
$conn =yii::app ()->db;
$command = $conn->createcommand ();
Direct Write statements:
$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 operation statements are similar in wording to the above, but with execute:


------Solution--------------------
In fact, simple crud operation, with ActiveRecord is not more convenient?
$model = User::model ()->findbypk (1);

$model->password = ' new password ';

$model->save ();
------Solution--------------------
Using Yii built-in methods
------Solution--------------------
Reference: in
fact, simple crud operation, with ActiveRecord is not more convenient?
$model = User::model ()->findbypk (1);
$model->password = ' new password ';
$model->save ();


And crud can be generated automatically, command line with YIIC shell or with visual interface GII are very convenient ah, first model, 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.