官方提供两种操作数据库的方法,使用Db类和助手函数db()。使用Db类时,需要指定命名空间use think\Db
,使用助手函数时,可以不用指定命名空间。以下以mysql
数据库为例进行测试练习。
tp5
tp_data
,建表sql
语句如下DROP TABLE IF EXISTS `tp_data`;
CREATE TABLE `tp_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`create_time` varchar(255) DEFAULT NULL,
`type` tinyint(1) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_data
-- ----------------------------
INSERT INTO `tp_data` VALUES ('1', 'xiao1', '1573887741', '0');
INSERT INTO `tp_data` VALUES ('2', 'xiao1', '1573887741', '0');
INSERT INTO `tp_data` VALUES ('3', 'xiao1', '1573887741', '0');
database.php
// +----------------------------------------------------------------------
return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'tp5',
// 用户名
'username' => 'root',
// 密码
'password' => 'root',
// 端口
'hostport' => '3306',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 'tp_',
// 数据库调试模式
'debug' => true,
];
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$time = time();
$rel = Db::execute("INSERT INTO `tp_data`(`id`,`name`,`create_time`) VALUES (null,'xiaode',{$time}),(null,'rufeike',{$time})");
dump($rel);
}
}
?>
输出结果
int(2)
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$time = time();
$data = Db::query("SELECT * FROM `tp_type`");
dump($data)
}
}
?>
输出结果
array(8) {
[0] => array(3) {
["id"] => int(1)
["name"] => string(4) "lldd"
["create_time"] => string(12) "154545415454"
}
[1] => array(3) {
["id"] => int(2)
["name"] => string(6) "xiaode"
["create_time"] => string(10) "1573876216"
}
}
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$table = Db::table('tp_data');
$table = Db::name('data');
}
}
Db::table()
和Db::name()
namespace app\index\controller;
use \think\Controller;
class Index extends Controller{
public function index(){
$db = db();
$rel= db('data')->insertGetId(array('name'=>'xiao','create_time'=>time()));
dump($rel);
}
}
输出结果
string(2) "16"
array('字段1'=>'字段值2','字段2'=>'字段值2'...)
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$rel= Db::table('tp_data')->insert(array('name'=>'xiao','create_time'=>time()));
dump($rel);
}
}
输出结果
int(1)
array('字段1'=>'字段值2','字段2'=>'字段值2'...)
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$rel= Db::table('tp_data')->insertGetId(array('name'=>'xiao','create_time'=>time()));
dump($rel);
}
}
输出结果
string(2) "15"
array(
array(‘字段1’=>‘字段值2’,‘字段2’=>‘字段值2’…),array('字段1'=>'字段值2','字段2'=>'字段值2'...))
;
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$data = array(
array('name'=>'xiao1','create_time'=>time()),
array('name'=>'xiao2','create_time'=>time()),
array('name'=>'xiao3','create_time'=>time()),
);
$rel= db('data')->insertAll($data);
dump($rel);
}
}
输出结果
int(3)
链式操作末端方法,根据指定条件进行更新操作,或者跟新数据中指定只增主键的值
参数格式array('字段1'=>'字段值2','字段2'=>'字段值2'...)
返回影响行数
namespace app\index\controller;
use \think\Controller;
class Index extends Controller{
public function index(){
//方式一
$data = array('name'=>'xiao1','create_time'=>time());
$rel= db('data')->where('id',10)->update($data);
//方式二
$data = array('id'=>10,'name'=>'xiao1','create_time'=>time());
$rel= db('data')->update($data);
dump($rel);
}
}
输出结果
int(1)
detele(主键)
和detele(array(主键1,主键2,主键3...))
namespace app\index\controller;
use \think\Controller;
class Index extends Controller{
public function index(){
$rel = db('data')->where(array('id'=>array('eq',10)))->delete();//方式1
$rel = db('data')->where('id',10)->update($data);//方式2
$rel = db('data')->delete(11);//方式3
$rel = db('data')->delete(12,13);//方式4
dump($rel);
}
}
find(主键)
find()
参数中指定主键
namespace app\index\controller;
use \think\Controller;
class Index extends Controller{
public function index(){
$rel1 = db('data')->find();//方式1
$rel2 = db('data')->find(3);//方式2
$rel3 = db('data')->where('id','lt',5)->find();
dump($rel1);
dump($rel2);
dump($rel3);
}
}
输出结果
array(3) {
["id"] => int(1)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
array(3) {
["id"] => int(3)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
array(3) {
["id"] => int(1)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
namespace app\index\controller;
use \think\Controller;
class Index extends Controller{
public function index(){
$rel1 = db('data')->select();//方式1
$rel2 = db('data')->select(array(1,2,3));//方式2
$rel3 = db('data')->where('id','lt',5)->select();//方式3
dump($rel1);
dump($rel2);
dump($rel3);
}
}
输出结果
array(19) {
[0] => array(3) {
["id"] => int(1)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[1] => array(3) {
["id"] => int(2)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[2] => array(3) {
["id"] => int(3)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
...
}
array(3) {
[0] => array(3) {
["id"] => int(1)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[1] => array(3) {
["id"] => int(2)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[2] => array(3) {
["id"] => int(3)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
}
array(4) {
[0] => array(3) {
["id"] => int(1)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[1] => array(3) {
["id"] => int(2)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[2] => array(3) {
["id"] => int(3)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[3] => array(3) {
["id"] => int(4)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
}