Home >Backend Development >PHP Tutorial >yii ,sql, 增删改查
SQL Yii 数据的增删改查 框架
[color=#0000FF]1.$sql= "insert into tbl_user(`id`,`username`,`password`)values(null,'alu','123'); 好象回过你一个贴子,再回一次吧:
utf8编码写到配置文件main.php中:'db'=>array('connectionString' => 'mysql:host=localhost;dbname=test','emulatePrepare' => true,'username' => 'root','password' => '123','charset' => 'utf8', //此处设定编码'tablePrefix' => 'tbl_',),控制器中: $conn=Yii::app()->db; $command=$conn->createCommand();直接写语句: $command->text='select * from {{user}} where id=1'; $dataReader=$command->query();读取数据: foreach($dataReader as $row) { echo $row['name']; } INSERT, UPDATE 和 DELETE 操作语句写法和上面类似,不过用execute: $rowCount=$command->execute();返回影响行数;
其实简单的CRUD操作,用ActiveRecord不是更方便吗?
$model = User::model()->findByPk(1);
$model->password = 'new password';
$model->save();
用yii内置的方法
其实简单的CRUD操作,用ActiveRecord不是更方便吗?
$model = User::model()->findByPk(1);
$model->password = 'new password';
$model->save();
而且CRUD都可以自动生成,命令行用yiic shell或者用可视界面gii都很方便啊,先model,再crud