Home > Backend Development > PHP Tutorial > yii框架,增删改查,sql解决思路

yii框架,增删改查,sql解决思路

WBOY
Release: 2016-06-13 11:46:03
Original
901 people have browsed it

yii框架,增删改查,sql
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][/color]
------解决方案--------------------
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();返回影响行数;
------解决方案--------------------
http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.arr
yii官网的中文手册!
看一遍再实地动手一下基本就会了!
------解决方案--------------------
了解一下Yii的ActiveRecord和Model吧,Yii对于数据库操作都有相关的方法的。建议看看手册。推荐网站http://www.yiiframework.com/doc/guide/1.1/zh_cn
------解决方案--------------------
推荐看http://www.yiibook.com,第三本貌似有翻译过的
习惯上用Yii的话数据库设计和基础数据入库可以使用DB Migration
或者就是建好表之后直接用Gii生成相应的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