Blogger Information
Blog 37
fans 1
comment 0
visits 27108
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1231_数据库操作 第44课
叮叮当当
Original
763 people have browsed it

1、DB facade原始查询

  1. # 返回结果集
  2. $res = DB::select( 'select * from posts where id>:id', ['id'=>25] );
  3. # 返回bool值
  4. $res = DB::insert( 'insert into posts(`title`,`content`) values(?,?)' ,["世界最长...","中新社福州12月22日电..."]);
  5. # 返回受影响数
  6. $res = DB::update( 'update posts set updated_at=? where id=28',[date('Y-m-d h:m:s',time())] );
  7. # 返回受影响数
  8. $res = DB::delete( 'delete from posts where id=?',[30] );

2、查询构造器

  1. # 返回结果集
  2. $res = DB::table('zans')->get()->toArray();
  3. # 返回bool值
  4. $res = DB::table('zans')->insert( ['user_id'=>6, 'post_id'=>28] );
  5. # 返回受影响数
  6. $res = DB::table('zans')->where('id',8)->update( ['created_at'=>date('Y-m-d h:m:s',time())] );
  7. # 返回受影响数
  8. $res = DB::table('zans')->where('id',11)->delete();

ps: 查询构造器里的select()是指定查询的列,DB facade原始查询里select()就是查询方法

3、where方法、whereIn方法、whereBetween方法、orWhere方法

  1. # 查询 id=19 的记录
  2. $res = DB::table('posts')->where( 'id', 19 )->get()->toArray();
  3. # 查询 id between (25,27) 的记录
  4. $res = DB::table('posts')->whereBetween( 'id', [25,27] )->get()->toArray();
  5. # 查询 id in (19,27) 的记录
  6. $res = DB::table('posts')->whereIn('id', [19,27])->get()->toArray();
  7. # 查询 id=27 or id=25 的记录
  8. $res = DB::table('posts')->orWhere('id',27)->orWhere('id',25)->get()->toArray();

4、insert 插入一条记录和多条记录的方法,并说明和insertGetId方法的异同

  1. # 插入一条记录,返回bool值
  2. $res = DB::table('zans')->insert(['user_id'=>6,'post_id'=>28]);
  3. # 插入多条记录,返回bool值
  4. $res = DB::table('zans')->insert([['user_id'=>6,'post_id'=>19],['user_id'=>3,'post_id'=>28]]);
  5. # 插入一条记录,返回新增记录的id值
  6. $res = DB::table('zans')->insertGetId(['user_id'=>1,'post_id'=>23]);

ps: insert和insertGetId方法异同:
同:都是插入数据
异:insert可插入多条,insertGetId只能插入一条;在插入一条记录时,insert返回bool值,insertGetId返回新增记录的id值

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:继续努力
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post