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(sql,
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(sql,
map);return′成功更新了′.a.‘条数据!’;
}
//查询构造器
//table()设置数据表
//field()设置查询字段列表
//select()返回满足条件的多条记录
//find()返回满足条件的第一条参数
public function demo3()
{
res=Db::table(′user′)−>field("userid,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);
}?>
The above is the detailed content of 关于thinkphp6.0.2实现对数据库操作. For more information, please follow other related articles on the PHP Chinese website!