ThinkPHP6之数据库操作上


前言

注意,tp6在进行语法学习的时候都是在app/index.php中写代码的,代码写在index函数下面,而且tp6自带的文件都是由自动加载器的,不需要包含autoload.php文件


1. 数据库配置

要对数据库进行操作,要修改两个地方,一个数.env文件,一个是config/database.php文件

  • config/database.php
 'connections'     => [
        'mysql' => [  // 代表连接的一个数据库
            // 数据库类型
            'type'            => env('database.type', 'mysql'),
            // 服务器地址
            'hostname'        => env('database.hostname', '127.0.0.1'),
            // 数据库名
            'database'        => env('database.database', 'cr'),
            // 用户名
            'username'        => env('database.username', 'root'),
            // 密码
            'password'        => env('database.password', '901026'),
            // 端口
            'hostport'        => env('database.hostport', '3306'),
            // 数据库连接参数
            'params'          => [],
            // 数据库编码默认采用utf8
            'charset'         => env('database.charset', 'utf8'),
            // 数据库表前缀   配置后只有写数据库后面
            'prefix'          => env('database.prefix', ''),

            // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
            'deploy'          => 0,
            // 数据库读写是否分离 主从式有效
            'rw_separate'     => false,
            // 读写分离后 主服务器数量
            'master_num'      => 1,
            // 指定从服务器序号
            'slave_no'        => '',
            // 是否严格检查字段是否存在
            'fields_strict'   => true,
            // 是否需要断线重连
            'break_reconnect' => false,
            // 监听SQL
            'trigger_sql'     => env('app_debug', true),
            // 开启字段缓存
            'fields_cache'    => false,
        ],
    ],

在这里插入图片描述

connections里面的一个子元素代表连接的一个数据库,要使用多个数据库,要添加多个数据库对象,写prefix之后就只需要写数据表后面的字段了

  • config/env
[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = cr
USERNAME = root
PASSWORD = 901026
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true

在这里插入图片描述

注意和config/database.php对应上就可以了

2. 数据库操作

创建数据库

create database cr;
use cr;
insert into phpcn_user values (1, '欧阳克'),(2, '朱'),(3,'asd'),(4,'asde'),(5,'rts');

在写操作数据库语句之前写use think\facade\Db;

1. 查询操作

  1. find():通过主键查找,find方法查询结果不存在,返回 null,否则返回结果数组

  2. where():查询的条件

  3. field(), column():查询某一列数据

  4. value() :通过值查找,查询某段的值

如上都是查询一条数据,一列数据,或者是一个数据

  1. select() :查询好多条数据,和toArray()一起使用
        $result = Db::table('phpcn_user')->column('id');
        printf('<pre>%s</pre>',print_r($result,true));
        $result = Db::table('phpcn_user')->where('id','>','2')->value('name');
        printf('<pre>%s</pre>', print_r($result, true));
        $result = Db::table('phpcn_user')->where('id', '3')->value('name');
        printf('<pre>%s</pre>', print_r($result, true));

        print('-----------------');
        $result = Db::table('phpcn_user')->where('id','>=','2')->select()->toArray();
        printf('<pre>%s</pre>', print_r($result, true));

2. 插入操作

  1. insert($data) $data为关联数组,通常通过
  2. insertAll($data) :添加多条数据
  3. insertGetId($data) :添加数据并且获取添加数据的主键
        $data = ['id'=>11,'name'=>'zxasdc'];
        $result = DB::table('phpcn_user')->insert($data);
        echo $result;
        $data = ['id'=>11,'name'=>'zxasdc'];
        $result = DB::table('phpcn_user')->insertGetId($data);
        echo $result;
        $data = [
            [
                'id'=>7,
                'name'=>'dfg'
            ],
            [
                'id' => 8,
                'name' => 'df8g'
            ]
            ];
        $result = DB::table('phpcn_user')->insertAll($data);
        echo $result;

插入操作,插入的都是关联数组

3. 修改

  1. update(修改后的值) + where(修改哪一个)
  2. updata($data) $data为关联数组
  3. inc():自增 dec():自减
 $result = Db::table('phpcn_user')->where('id',3)->update(['name'=>'ykk']);

4. 删除

  1. delete()
  2. useSoftDelete()
$result = Db::table('phpcn_user')->where('id',5)->delete();

5. 其他

  1. save($data) : $data是关联数组,save方法统一写入数据,自动判断是新增还是更新数据(以写入数据中是否存在主键数据为依据)
  2. query() :方法用于执行 MySql 查询操作
  3. execute() : 方法用于执行 MySql 新增和修改操作

2和3都是直接操作数据库的

        $result = Db::query("select * from `phpcn_user` where `id` > 3");
        print_r($result);
        $result = Db::execute("insert into `phpcn_user` values (11, 'asd')");
        print_r($result);

3.数据集

  1. toArray()
  2. isEmpty()

总结

数据库操作前一定要加上 use think\facade\Db;

猜你喜欢

转载自blog.csdn.net/qq_53568983/article/details/128337865