溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

thinkphp6.0.2實(shí)現(xiàn)對(duì)數(shù)據(jù)庫操作的示例

發(fā)布時(shí)間:2021-01-19 10:38:44 來源:億速云 閱讀:181 作者:小新 欄目:編程語言

小編給大家分享一下thinkphp6.0.2實(shí)現(xiàn)對(duì)數(shù)據(jù)庫操作的示例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

因?yàn)榘姹靖聦?dǎo)致有些沒入門的同學(xué)學(xué)習(xí)thinkphp6出現(xiàn)問題,在這里保留我學(xué)習(xí)的一點(diǎn)經(jīng)驗(yàn)!

這是app/contrlar/目錄下新建的文件DbTest

<?php
namespace app\controller;//要讓其控制器顯示
use think\facade\Db;//使用哪個(gè)基類要包含其路徑

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.‘條數(shù)據(jù)!’;
}
//查詢構(gòu)造器
//table()設(shè)置數(shù)據(jù)表
//field()設(shè)置查詢字段列表
//select()返回滿足條件的多條記錄
//find()返回滿足條件的第一條參數(shù)
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()設(shè)置查詢條件,參數(shù)包括字符串,表達(dá)式,數(shù)組
//fetchsql():true,false
public function demo4()
{
res=Db::table(user)?>field("userid,name,age")//?>where(userid=3)//?>where(userid,=,1)達(dá)//?>where(age,>,20)//?>where(age,between,[20,30])區(qū)//?>where([userid=>2,age=>20])數(shù)?>where([[age,between,[20,30]]])//數(shù)?>select();dump(res=Db::table('user')            ->field("user_id,name,age")            //->where('user_id=3')字符串            //->where('user_id','=','1')表達(dá)式            //->where('age','>','20')                //->where('age','between',[20,30])區(qū)間查詢                //->where(['user_id'=>2,'age'=>20])數(shù)組查詢                ->where([['age','between',[20,30]]])//索引數(shù)組查詢            ->select();        dump(res);
}?>

比起以往版本這個(gè)無需設(shè)置路由,數(shù)據(jù)庫也有原生文檔Env可以修改,想要添加新文檔首先在config/app.php里面將應(yīng)用變量指向你的新文檔然后打開瀏覽器輸入:

服務(wù)器名/類名/函數(shù)名/

即可訪問了

thinkphp6.0.2實(shí)現(xiàn)對(duì)數(shù)據(jù)庫操作的示例

以上是“thinkphp6.0.2實(shí)現(xiàn)對(duì)數(shù)據(jù)庫操作的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI