數(shù)據(jù)庫增刪改查

連接數(shù)據(jù)庫

修改(C:\AppServ\www\tp5\application\database.php)

   // 數(shù)據(jù)庫類型
    'type'            => 'mysql',
    // 服務(wù)器地址
    'hostname'        => '127.0.0.1',
    // 數(shù)據(jù)庫名
    'database'        => 'yzmedu',
    // 用戶名
    'username'        => 'root',
    // 密碼
    'password'        => '8096233.',
    // 端口
    'hostport'        => '3306',

前臺(tái)控制器中

use think\Db;
   public function data(){
        //實(shí)例化系統(tǒng)數(shù)據(jù)庫
        $DB= new Db;
 //方法一 查詢數(shù)據(jù) 系統(tǒng)方法
        $data=$DB::table("user")->select();
 //方法二 使用sql語句
        $data=$DB::query("select*from user");
        dump($data);
    }


操作

//聲明命名空間
namespace app\index\controller;
//導(dǎo)入系統(tǒng)控制器類
use think\Controller;
//引入系統(tǒng)類
use think\Db;
//聲明控制器類
class User extends Controller{
......

.....
}

瀏覽器訪問http://localhost/tp5/public/index.php/index/User/select
1、增

public function insert(){
       //執(zhí)行查詢語句
       //返回值為影響行數(shù)
       $data=Db::execute("insert into user value(null,'user','123','66')");
       $data=Db::execute("insert into user value(null,?,?,?)",['user2','456','20']);
       $data=Db::execute("insert into user value(null,:name,:pass,:age)",['name'=>"user3",'pass'=>"678",'age'=>"224"]);
       dump($data);
   }

2、刪

  public function delete(){
        //返回值影響行數(shù)
        //$data=Db::execute('delete from user where id=10');
       // $data=Db::execute("delete from user where id>?",[15]);
        $data=Db::execute("delete from user where id>:id",["id"=>5]);
        dump($data);
    }

3、改

  //改
    public function update(){
        $data=Db::execute("update user set age='20' where id=9");
        dump($data);
    }

4、查

 public function select(){
        //查詢數(shù)據(jù)
        $data=DB::query("select*from user");
        //dump($data);
        $data=Db::query("select*from user where id>=? and id<=?",[5,8]);
        dump($data);
    }

5、獲取執(zhí)行的sql語句
echo Db::getLastSql();

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容