thinkphp5.0学习笔记(四)数据库的操做

ThinkPHP内置了抽象数据库访问层,把不一样的数据库操做封装起来,咱们只须要使用公共的Db类进行操做,而无需针对不一样的数据库写不一样的代码和底层实现,Db类会自动调用相应的数据库驱动来处理。采用PDO方式,目前包含了Mysql、SqlServer、PgSQL、Sqlite等数据库的支持。sql

1.基本使用

配置了数据库链接信息后,咱们就能够直接使用数据库运行原生SQL操做了,支持query(查询操做)和execute(写入操做)方法,而且支持参数绑定。数据库

public function read() { $sql = Db::query('select * from news'); dump($sql); }

输出的是:数组

execute方法:闭包

public function read() { $sql = Db::execute('insert into news (nid, rid) values (11, 11)');; dump($sql); }

输出:spa

数据库添加成功!3d

也支持命名占位符绑定,例如:code

public function read() { $sql = Db::query('select * from news where nid=:nid',['nid'=>1]); dump($sql); }

输出:blog

execute方法:get

public function read() { $sql = Db::execute('insert into news (nid, rid) values (:nid, :rid)',['nid'=>18,'rid'=>'121']); dump($sql); }

输出:it

数据库添加成功!

 

能够使用多个数据库链接:

2.查询构造器

听名字就知道,很装X..

先来看基本查询

查询一个数据:

 // table方法必须指定完整的数据表名
    $sql =Db::table('news')->where('nid',1)->find(); dump($sql);

find = 查询一条;而且查询结果不存在,返回 null

输出:

Db::table('think_user')->where('status',1)->select();

这条查询语句与上面同效,可是select 方法查询结果不存在,返回空数组

额 这个玩意叫查询数据集,没错!

默认状况下,find和select方法返回的都是数组。

 

若是你要查询某个字段的值,咋整?

public function read() { // 返回某个字段的值
    $sql =Db::table('news')->where('nid',18)->value('rid'); dump($sql); }

这样看输出,我求rid的值:

 

若是你须要处理成千上百条数据库记录,能够考虑使用chunk方法,该方法一次获取结果集的一小块,而后填充每一小块数据到要处理的闭包,该方法在编写处理大量数据库记录的时候很是有用。

public function read() { $sql =Db::table('news')->chunk(1,function($user){ foreach($user as $u) { dump($u); } }); }

这个样子  就能够一条一条都给遍历出来了!

是“一条一条·”,嘿!

 

3.添加数据跟删除数据

使用 Db 类的 insert 方法向数据库提交数据

public function read() { $data = ['ntitle' => '123', 'rid' => '456']; $sql = Db::table('news')->insert($data); dump($sql); }

添加成功后insert 方法返回添加成功的条数,insert 正常状况返回 1

添加数据后若是须要返回新增数据的自增主键,能够使用getLastInsID方法:

public function read() { $data = ['ntitle' => '123', 'rid' => '345']; $sql = Db::table('news')->insert($data); $userId = Db::name('news')->getLastInsID('nid'); dump($userId); dump($sql); }

看输出:

主键字段22!

 

 添加多条数据:

添加多条数据直接向 Db 类的 insertAll 方法传入须要添加的数据便可;

public function read() { $data = [ ['ntitle' =>'gaga','rid' => '12'], ['ntitle' =>'gaaaga','rid' => '123'] ]; $sql = Db::table('news')->insertAll($data); dump($sql); }

这样的话,返回的应该是两条2

删除数据:

根据主键来删除

public function read() { // 根据主键 来删
$sql = Db::table('news')->delete(1); //多删 //$sql = Db::table('news')->delete(1,2,3);
dump($sql); }

执行成功返回影响行数;

还有一种是根据条件来删除

public function read() { // 根据条件 来删
$sql = Db::table('news')->where('nid',18)->delete(); //多删 //$sql = Db::table('news')->where('nid','<',1)->delete();
dump($sql); }

执行成功也是返回影响行数;

4.查询方法:

where方法:

能够使用where方法进行AND条件查询:

public function read() { $sql = Db::table('news') ->where('nid',20) ->where('ntitle',123) ->find(); dump($sql); }

whereOr方法:

使用whereOr方法进行OR查询:

public function read() { $sql = Db::table('news') ->where('nid',20) ->whereOr('ntitle','like','%123%') ->find(); dump($sql); }

混合查询:

where方法和whereOr方法在复杂的查询条件中常常须要配合一块儿混合使用

public function read() { $sql = Db::table('news') ->where(function($query){ $query->where('nid',21)->where('nid',22); }) ->whereOr(function($query) { $query->where('ntitle','123')->whereOr('ntitle','123'); }) ->select(); dump($sql); }

输出的是:

看一下生成的代码:

SELECT * FROM `news` WHERE  (  `nid` = 1 OR `nid` = 2 ) OR (  `ntitle` LIKE '123' OR `ntitle` LIKE '123' )

第一个查询方法用where或者whereOr是没有区别的。

相关文章
相关标签/搜索