ThinkPHP5.1框架數(shù)據(jù)庫(kù)鏈接和增刪改查操作示例
本文實(shí)例講述了ThinkPHP5.1框架數(shù)據(jù)庫(kù)鏈接和增刪改查操作。分享給大家供大家參考,具體如下:
一、數(shù)據(jù)庫(kù)的鏈接方式
<?php namespace app\index\controller; use think\Db; class Demo { //1、全局配置 config/database.php配置 public function dbTest() { return Db::table('pzq_article') ->where('id','29') ->value('title'); } //2、動(dòng)態(tài)配置 think\db\Query.php中有一個(gè)方法connect() public function dbTest2() { return Db::connect([ 'type'=>'mysql', 'hostname'=>'localhost', 'database'=>'top789', 'username'=>'root', 'password'=>'root', ]) ->table('pzq_article') ->where('id','76') ->value('title'); } //3、DSN連接 public function dbTest3() { $dsn = 'mysql://root:root@localhost:3306/top789#utf8'; return Db::connect($dsn) ->table('pzq_article') ->where('id','88') ->value('title'); } //4、單條查旬 public function dbTest4() { $res = Db::table('pzq_article') ->field(['title'=>'標(biāo)題','id'=>'編號(hào)'])//可添加別名 ->where('id','=',20)//如果是等號(hào),=可以省略 ->find();//如果是主鍵查詢,可省略上面where,這行寫(xiě)->find(20); dump(is_null($res)?'沒(méi)有查到':$res); } //5、多條查旬 public function dbTest5() { $res = Db::table('pzq_article') ->field(['id','cat_id','title']) ->where([ ['id','>',20], ['cat_id','=',2], ])//一個(gè)條件,直接用表達(dá)式->where('id','>',20)。多個(gè)條件用數(shù)組 ->order('id desc')->limit(3)->select(); if(empty($res)){ return '沒(méi)有查到'; }else{ dump($res); } } //6、數(shù)據(jù)添加 public function dbTest6() { $data = [ 'name'=>'Sam2', 'age'=>'29', 'posttime'=>time() ]; $dataall=[ ['name'=>'Sam3','age'=>'29','posttime'=>time()], ['name'=>'Sam4','age'=>'30','posttime'=>time()], ]; //(1)單條插入 //return Db::table('test')->data($data)->insert(); //(2)插入同時(shí)返回新增主鍵id //return Db::table('test')->insertGetId($data); //(3)插入多條數(shù)據(jù) return Db::table('test')->data($dataall)->insertAll(); } //更新數(shù)據(jù) public function dbTest7() { // return Db::table('test') // ->where('id','=',4) // ->update(['name'=>'SamC','age'=>'31']); //如果where條件是主鍵,還可以如下使用 return Db::table('test') ->update(['name'=>'SamCheng','age'=>'30','id'=>4]); } //刪除操作 public function dbTest8() { //return Db::table('test')->delete(6); //或者 return Db::table('test')->where('id',5)->delete(); } //mysql原生語(yǔ)句 查詢 public function dbTest9() { $sql = "select name,age from test where id>2"; dump(Db::query($sql)); } //mysql 增、刪、改 用Db::execute($sql) public function dbTest10() { //$sql = "update test set name='samC' where id=4"; //$sql = "insert test set name='Yan',age='30'"; $sql = "delete from test where id=4"; return Db::execute($sql); } }
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門(mén)教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門(mén)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
- tp5.1框架數(shù)據(jù)庫(kù)子查詢操作實(shí)例分析
- tp5.1 框架數(shù)據(jù)庫(kù)常見(jiàn)操作詳解【添加、刪除、更新、查詢】
- tp5.1 框架數(shù)據(jù)庫(kù)高級(jí)查詢技巧實(shí)例總結(jié)
- PHP利用pdo_odbc實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)示例【基于ThinkPHP5.1搭建的項(xiàng)目】
- PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫(kù)示例【基于thinkPHP5.1框架】
- ThinkPHP實(shí)現(xiàn)多數(shù)據(jù)庫(kù)連接的解決方法
- thinkPHP5實(shí)現(xiàn)的查詢數(shù)據(jù)庫(kù)并返回json數(shù)據(jù)實(shí)例
- tp5(thinkPHP5)框架數(shù)據(jù)庫(kù)Db增刪改查常見(jiàn)操作總結(jié)
- tp5(thinkPHP5)框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)查詢的方法
- tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫(kù)的方法
- thinkPHP5實(shí)現(xiàn)數(shù)據(jù)庫(kù)添加內(nèi)容的方法
- tp5.1 框架數(shù)據(jù)庫(kù)-數(shù)據(jù)集操作實(shí)例分析
相關(guān)文章
詳解Laravel5.6通過(guò)路由進(jìn)行API版本控制的簡(jiǎn)單方法
這篇文章主要介紹了詳解Laravel5.6通過(guò)路由進(jìn)行API版本控制的簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01使用php偽造referer的方法 利用referer防止圖片盜鏈
當(dāng)瀏覽器向web服務(wù)器發(fā)送請(qǐng)求的時(shí)候,一般會(huì)帶上Referer,告訴服務(wù)器我是從哪個(gè)頁(yè)面鏈接過(guò)來(lái)的,服務(wù)器籍此可以獲得一些信息用于處理,不過(guò)這個(gè)Referer是可以偽造,下面看一個(gè)示例,大家就明白了2014-01-01