ThinkPHP之数据库操作

连接数据库

在应用目录或模块目录的database.php配置数据库信息。

return [
    // 数据库类型
    'type'            => 'mysql',
    // 服务器地址
    'hostname'        => '127.0.0.1',
    // 数据库名
    'database'        => '',
    // 用户名
    'username'        => '',
    // 密码
    'password'        => '',
    // 端口
    'hostport'        => '',
    // 连接dsn
    'dsn'             => '',
    // 数据库连接参数
    'params'          => [],
    // 数据库编码默认采用utf8
    'charset'         => 'utf8',
    // 数据库表前缀
    'prefix'          => '',
    // 数据库调试模式
    'debug'           => true,
    // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
    'deploy'          => 0,
    // 数据库读写是否分离 主从式有效
    'rw_separate'     => false,
    // 读写分离后 主服务器数量
    'master_num'      => 1,
    // 指定从服务器序号
    'slave_no'        => '',
    // 自动读取主库数据
    'read_master'     => false,
    // 是否严格检查字段是否存在
    'fields_strict'   => true,
    // 数据集返回类型
    'resultset_type'  => 'array',
    // 自动写入时间戳字段
    'auto_timestamp'  => false,
    // 时间字段取出后的默认时间格式
    'datetime_format' => 'Y-m-d H:i:s',
    // 是否需要进行SQL性能分析
    'sql_explain'     => false,
];

 

原生SQL操作

使用前引入数据库操作类

use think\Db;

查询操作 

Db::query('select * from user where id=?',[1]);

 插入操作

Db::execute('insert into user(username,password) values(?,?)',[$username,$password]);

查询构造器

where查询

查询一个数据,find方法查询不存在时返回null

Db::table('article')
    ->where(id,1)
    ->find();

查询一个数据集,select()方法查询不存在时返回一个空数据

Db::table('article')
    ->where('user','brell')
    ->select();
Db::table('article')
    ->where('id','<',5)
    ->select();

field方法:返回操作的字段

Db::table('article')
    ->filed('title','content')
    ->select();

order方法:结果排序

Db::table('article')
    ->where('id','>',5)
    ->order('create_time')
    ->limit(10)
    ->select();

fetchSql方法:返回sql语句

$sql=Db::table('article')->fetchSql(true)->find(1);

union方法:合并两个或多个SELECT语句的结果集

Db::field('name')
    ->table('user0')
    ->union(select name from user1)
    ->union(select name from user2)
    ->select();

limit方法:指定查询和操作的数据

Db::table('article')
    ->where('id>10')
    ->filed('title,content')
    ->limit(10)
    ->select()
Db::table('article')
    ->filed('title,content')
    ->limit(10,20)
    ->select()

page方法

DB::table('article')
    ->page(1,10)
    ->select();

增加数据构造器

增加一条数据

$data=['title'=>'title1','content'=>'content1'];
Db::table('article')
    ->insert($data);

增加多条数据

$data=[
       ['title'=>'title1','content'=>'content1'],
       ['title'=>'title2','content'=>'content2']
];
Db::table('article')
    ->insertAll($data);

删除数据构造器

根据主键删除

Db::table('article')
    ->delete(1);

Db::table('article') 
    ->delete([2,3,4]);

根据条件删除

Db::table('article')
    ->where('id',1)
    ->delete();

Db::table('article')
    ->where('id','<',10)
    ->delete();

更新数据构造器

Db::table('article')
    ->where('id',1)
    ->update(['title'=>'learn php']);

或者

Db::table('article')
    ->update(['title'=>'learn php','id'=>1]);

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值