Thinkphp6框架学习:有关数据库的基本操作

时间:2019-09-17
本文章向大家介绍Thinkphp6框架学习:有关数据库的基本操作,主要包括Thinkphp6框架学习:有关数据库的基本操作使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

最近Thinkphp6框架出来了,Mysql 8.0也出来了,php版本也升级到了7.4(这里php使用的是php7.3)

为了赶上时代的潮流,连ide(phpstorm)也升级到了2019.2的版本

以下是本机所使用的环境及版本(phpstudy也是新ui 8.0)

对于数据库的配置在框架中app\config\database.php中已经配置好了,所以下面将默认已经连接上了数据库

作为例子展示的数据表:

首先是

1.原生查询操作的读操作:query()

public function demo1()
    {
        $sql = "SELECT `userName` FROM `admin` WHERE `id`=:id ";
        $map = ['id' => 1];
        $res = Db::query($sql, $map);
        dump($res);//打印查询结果$res
    }

打印结果:

2.原生查询操作的写操作:insert/update/delete,execute()

public function demo2()
    {
        $sql = "UPDATE `admin` SET `status`=:status WHERE `id`=:id ";
        $map = ['id' => 1, 'status' => 0];
        $res = Db::execute($sql, $map);
        return '成功更新了'.$res.'条记录';
    }

结果:

数据表中 id=1 的 status从1变为0

3.查询构造器

① find() : 返回满足条件的第一条记录,单条记录;无返回null

//table():设置数据表
    //field():设置查询字段列表
    public function demo3()
    {
        $res = Db::table('admin')
            ->field('id, userName, phone')
            ->find(2);//支持将主键作为参数:WHERE `id` = 2
        dump($res);
    }

结果:

②select() : 返回满足条件的多条记录

public function demo4()
    {
        $res = Db::table('admin')
            ->field('id, userName, phone')
            //注:select(2, 3)只返回第一条;select([2, 3])应放入数组中
            ->select();//同上
        dump($res);
    }

结果:

4 . where()函数:可以根据其他字段查询

设置查询条件 主要类型:字符串,表达式,数组

(1)字符串

public function demo5()
    {
        $res = Db::table('admin')
            ->field('id, userName, phone')//field()仅返回对应字段

            //1.字符串
            ->where('status > 0')
            ->select();
        dump($res);
}

结果:

(2)表达式

public function demo5()
    {
        $res = Db::table('admin')
            ->field('id, userName, phone')//field()仅返回对应字段

            //2.表达式:推荐
            ->where('id','between',[1,3])//参数格式:字段,操作符,值
            ->select();
        dump($res);
}

结果:

(3)数组

①关联数组:等值查询,AND

public function demo5()
    {
        $res = Db::table('admin')
            ->field('id, userName, phone')//field()仅返回对应字段

           // ①关联数组
            ->where(['id' => 1, 'phone' => 123])
            ->select();
        dump($res);
}

打印sql语句看一下(在where()和select()中间插入下面代码)

->fetchSql(true)

这就是关联数组的等值查询(AND)

结果:

②索引数组:批量查询

public function demo5()
    {
        $res = Db::table('admin')
            ->field('id, userName, phone')//field()仅返回对应字段

          //②索引数组
            ->where([
                // ‘0=>’ 索引值可以忽略,默认值
                0 => ['id','between',[1,3]],
                1 => ['status','>',0]
            ])

            ->select();
        dump($res);
}

结果:

5.order(),limit()函数

①单字段排序

//order(),limit()
    public function demo6()
    {
        $res = Db::table('admin')
            ->field('id, userName, phone')

            //单字段排序
            ->order('id', 'desc')//默认升序asc,desc:降序
            ->limit(2,2)//从第2行开始的2条数据,常用于分页查询

            ->select();
        dump($res);
    }

结果:

②多字段排序,用数组参数

讲单字段排序中order()一行改为以下代码即可

->order(['id'=>'asc', 'phone'=>'desc'])

原文地址:https://www.cnblogs.com/yi2105/p/11532215.html