Home > PHP Framework > ThinkPHP > body text

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

藏色散人
Release: 2020-07-30 13:44:50
forward
3055 people have browsed it

下面由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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!