ThinkPHP内置了抽象数据库访问层,把不同的数据库操作封装起来,我们只需要使用公共的Db类进行操作,而无需针对不同的数据库写不同的代码和底层实现,Db类会自动调用相应的数据库驱动来处理。采用PDO方式,目前包含了Mysql、SqlServer、PgSQL、Sqlite等数据库的支持。
1.基本使用
配置了数据库连接信息后,我们就可以直接使用数据库运行原生SQL操作了,支持query
(查询操作)和execute
(写入操作)方法,并且支持参数绑定。
1 2 3 4 5 | public function read()
{ $sql = Db::query( 'select * from news' );
dump( $sql );
}
|
Salin selepas log masuk
输出的是:![](https://img.php.cn/upload/article/000/000/001/9a5785ebca28b8691d08b17bbbde5d7b-0.png)
execute方法:
1 2 3 4 | public function read()
{ $sql = Db::execute( 'insert into news (nid, rid) values (11, 11)' );;
dump( $sql );
}
|
Salin selepas log masuk
输出:![](https://img.php.cn/upload/article/000/000/001/9a5785ebca28b8691d08b17bbbde5d7b-1.png)
数据库添加成功!
也支持命名占位符绑定,例如:
1 2 3 4 | public function read()
{ $sql = Db::query( 'select * from news where nid=:nid' ,[ 'nid' =>1]);
dump( $sql );
}
|
Salin selepas log masuk
输出:![](https://img.php.cn/upload/article/000/000/001/9a5785ebca28b8691d08b17bbbde5d7b-2.png)
execute方法:
1 2 3 4 | public function read()
{ $sql = Db::execute( 'insert into news (nid, rid) values (:nid, :rid)' ,[ 'nid' =>18, 'rid' => '121' ]);
dump( $sql );
}
|
Salin selepas log masuk
输出:![](https://img.php.cn/upload/article/000/000/001/9a5785ebca28b8691d08b17bbbde5d7b-3.png)
数据库添加成功!
可以使用多个数据库连接:
2.查询构造器
听名字就知道,很装X..
先来看基本查询;
查询一个数据:
find = 查询一条;并且查询结果不存在,返回 null
输出:![](https://img.php.cn/upload/article/000/000/001/9a5785ebca28b8691d08b17bbbde5d7b-4.png)
1 | Db::table( 'think_user' )->where( 'status' ,1)->select();
|
Salin selepas log masuk
这条查询语句与上面同效,但是select 方法查询结果不存在,返回空数组
额 这个玩意叫查询数据集,没错!
默认情况下,find和select方法返回的都是数组。
如果你要查询某个字段的值,咋整?
1 2 3 4 | public function read()
{
dump( $sql );
}
|
Salin selepas log masuk
这样看输出,我求rid的值:
![](https://img.php.cn/upload/article/000/000/001/9a5785ebca28b8691d08b17bbbde5d7b-5.png)
如果你需要处理成千上百条数据库记录,可以考虑使用chunk方法,该方法一次获取结果集的一小块,然后填充每一小块数据到要处理的闭包,该方法在编写处理大量数据库记录的时候非常有用。
1 2 3 4 5 6 7 8 9 | public function read()
{ $sql =Db::table( 'news' )->chunk(1, function ( $user ){ foreach ( $user as $u )
{
dump( $u );
}
});
}
|
Salin selepas log masuk
这个样子 就可以一条一条都给遍历出来了!
是“一条一条·”,嘿!
3.添加数据跟删除数据
使用 Db
类的 insert
方法向数据库提交数据
1 2 3 4 5 | public function read()
{ $data = [ 'ntitle' => '123' , 'rid' => '456' ]; $sql = Db::table( 'news' )->insert( $data );
dump( $sql );
}
|
Salin selepas log masuk
添加成功后insert 方法返回添加成功的条数,insert 正常情况返回 1
添加数据后如果需要返回新增数据的自增主键,可以使用getLastInsID
方法:
1 2 3 4 5 | public function read()
{ $data = [ 'ntitle' => '123' , 'rid' => '345' ]; $sql = Db::table( 'news' )->insert( $data ); $userId = Db::name( 'news' )->getLastInsID( 'nid' );
dump( $userId );
dump( $sql );
}
|
Salin selepas log masuk
看输出:![](https://img.php.cn/upload/article/000/000/001/d8881e072d307470d541e9c8bf7e9c61-6.png)
主键字段22!
添加多条数据:
添加多条数据直接向 Db 类的 insertAll 方法传入需要添加的数据即可;
1 2 3 4 5 6 7 | public function read()
{ $data = [
[ 'ntitle' => 'gaga' , 'rid' => '12' ],[ 'ntitle' => 'gaaaga' , 'rid' => '123' ]
]; $sql = Db::table( 'news' )->insertAll( $data );
dump( $sql );
}
|
Salin selepas log masuk
这样的话,返回的应该是两条2
![](https://img.php.cn/upload/article/000/000/001/d8881e072d307470d541e9c8bf7e9c61-7.png)
删除数据:
根据主键来删除
执行成功返回影响行数;
还有一种是根据条件来删除的
执行成功也是返回影响行数;
4.查询方法:
where方法:
可以使用where
方法进行AND
条件查询:
1 2 3 4 | public function read()
{ $sql = Db::table( 'news' )->where( 'nid' ,20)->where( 'ntitle' ,123)->find();
dump( $sql );
}
|
Salin selepas log masuk
whereOr方法:
使用whereOr
方法进行OR
查询:
1 2 3 4 | public function read()
{ $sql = Db::table( 'news' )->where( 'nid' ,20)->whereOr( 'ntitle' , 'like' , '%123%' )->find();
dump( $sql );
}
|
Salin selepas log masuk
混合查询:
where方法和whereOr方法在复杂的查询条件中经常需要配合一起混合使用
1 2 3 4 5 6 7 | 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 );
}
|
Salin selepas log masuk
输出的是:![](https://img.php.cn/upload/article/000/000/001/d8881e072d307470d541e9c8bf7e9c61-8.png)
看一下生成的代码:
1 | SELECT * FROM `news` WHERE ( `nid` = 1 OR `nid` = 2 ) OR ( `ntitle` LIKE '123' OR `ntitle` LIKE '123' )
|
Salin selepas log masuk
第一个查询方法用where或者whereOr是没有区别的。
查询接五
Atas ialah kandungan terperinci thinkphp5.0学习笔记之数据库的操作. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!