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