yii ,sql, 增删改查,该怎么解决

WBOY
Release: 2016-06-13 11:45:40
Original
836 people have browsed it

yii ,sql, 增删改查
[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;";
//设置字符编码
mysql_query("set names utf8");
$result=mysql_query($sql) or die('sql语句错误,系统给的提示是:'.mysql_error());

5.$num = mysql_num_rows($result)以及$num=mysql_fetch_array($result);
以上语句在yii框架里面应该怎样写?
[/color]
------解决方案--------------------
好象回过你一个贴子,再回一次吧:

<br />utf8编码写到配置文件main.php中:<br />'db'=>array(<br />'connectionString' => 'mysql:host=localhost;dbname=test',<br />'emulatePrepare' => true,<br />'username' => 'root',<br />'password' => '123',<br />'charset' => 'utf8',  //此处设定编码<br />'tablePrefix' => 'tbl_',<br />),<br />控制器中:<br />  $conn=Yii::app()->db;<br />  $command=$conn->createCommand();<br />直接写语句:<br />  $command->text='select * from {{user}} where id=1';<br />  $dataReader=$command->query();<br />读取数据:<br />  foreach($dataReader as $row)<br />     { echo $row['name']; }<br /><br />  INSERT, UPDATE 和 DELETE 操作语句写法和上面类似,不过用execute:<br />  $rowCount=$command->execute();返回影响行数; <br />
Copy after login

------解决方案--------------------
其实简单的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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!