search
HomePHP FrameworkThinkPHP关于thinkphp6.0.2实现对数据库操作

关于thinkphp6.0.2实现对数据库操作

Jul 30, 2020 pm 01:44 PM
thinkphp6database

下面由thinkphp框架教程栏目给大家介绍thinkphp6.0.2实现对数据库操作的方法,希望对需要的朋友有所帮助!

关于thinkphp6.0.2实现对数据库操作

因为版本更新导致有些没入门的同学学习thinkphp6出现问题,在这里保留我学习的一点经验!

这是app/contrlar/目录下新建的文件DbTest

namespace app\controller;//要让其控制器显示
use think\facade\Db;//使用哪个基类要包含其路径

class DbTest
{
public function demo1()
{
//原生查询的读操作query()
$sql=“SELECT user_id,name,age,email FROM user WHERE age>:age  LIMIT :num”;
$map=array(“age”=>20,‘num’=>3);
a=Db::query(a=Db::query(sql,map);dump(map);        dump(a);
}
public function demo2()
{
//原生查询的写操作execute()
$sql=“UPDATE user SET age=:age WHERE user_id=:id”;
$map=array(“age”=>20,‘id’=>3);
a=Db::execute(a=Db::execute(sql,map);return.map);        return '成功更新了'.a.‘条数据!’;
}
//查询构造器
//table()设置数据表
//field()设置查询字段列表
//select()返回满足条件的多条记录
//find()返回满足条件的第一条参数
public function demo3()
{
res=Db::table(user)>field("userid,name,age")>find(2);dump(res=Db::table('user')            ->field("user_id,name,age")            ->find(2);        dump(res);
}
//where()设置查询条件,参数包括字符串,表达式,数组
//fetchsql():true,false
public function demo4()
{
res=Db::table(user)>field("userid,name,age")//>where(userid=3)//>where(userid,=,1)//>where(age,>,20)//>where(age,between,[20,30])//>where([userid=>2,age=>20])>where([[age,between,[20,30]]])//>select();dump(res=Db::table('user')            ->field("user_id,name,age")            //->where('user_id=3')字符串            //->where('user_id','=','1')表达式            //->where('age','>','20')                //->where('age','between',[20,30])区间查询                //->where(['user_id'=>2,'age'=>20])数组查询                ->where([['age','between',[20,30]]])//索引数组查询            ->select();        dump(res);
}?>

比起以往版本这个无需设置路由,数据库也有原生文档Env可以修改,想要添加新文档首先在config/app.php里面将应用变量指向你的新文档然后打开浏览器输入:

服务器名/类名/函数名/

即可访问了

企业微信截图_15960878791830.png

The above is the detailed content of 关于thinkphp6.0.2实现对数据库操作. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools