thinkPHP數(shù)據(jù)庫(kù)增刪改查操作方法實(shí)例詳解
本文實(shí)例講述了thinkPHP數(shù)據(jù)庫(kù)增刪改查操作方法。分享給大家供大家參考,具體如下:
thinkphp對(duì)數(shù)據(jù)庫(kù)增刪改查進(jìn)行了封裝操作,使得使用更加方便,但是不一定靈活。
可以用封裝的用,需要寫(xiě)sql,可以執(zhí)行sql。
1.原始的
$Model = new Model(); // 實(shí)例化一個(gè)model對(duì)象 沒(méi)有對(duì)應(yīng)任何數(shù)據(jù)表 $insert_sql = "INSERT INTO sh_wxuser_collection (user_id,store_id,good_id,addtime) VALUES('".$user_id."','".$store_id."','".$good_id."','".$addtime."');"; $Model - >query($insert_sql);
2.針對(duì)表實(shí)例化的,這里的表原名是sh_wxuser_collection。sh是前綴。
$model = M('wxuser_collection'); //自動(dòng)省去sh $insert_sql = "INSERT INTO __TABLE__ (user_id,store_id,good_id,addtime) VALUES('".$user_id."','".$store_id."','".$good_id."','".$addtime."');"; $model - >query($insert_sql);
另一種寫(xiě)法,_可以寫(xiě)成大寫(xiě),它會(huì)自動(dòng)轉(zhuǎn)化成_
$model = M('WxuserCollection'); //自動(dòng)省去sh $insert_sql = "INSERT INTO __TABLE__ (user_id,store_id,good_id,addtime) VALUES('".$user_id."','".$store_id."','".$good_id."','".$addtime."');"; $model - >query($insert_sql);
3. 封裝的add語(yǔ)句
$model = M('WxuserCollection'); $data = array('user_id' = >$user_id, 'store_id' = >$store_id, 'good_id' = >$good_id, 'addtime' = >$addtime); $model - >data($data) - >add();
4.封裝的修改edit語(yǔ)句
$model = M('WxuserCollection'); $data = array('user_id' = >$user_id, 'store_id' = >$store_id, 'good_id' = >$good_id, 'addtime' = >$addtime); $model - >data($data) - >where('id=3') - >save();
確實(shí)挺方便的,但是方便之余,別忘了原始的sql,原汁原味的sql,才最有意思。
5.find()
$model = M('WxuserCollection'); $res1 = $model - >find(1); $res2 = $model - >find(2); $res3 = $model - >where('good_id=1105 AND store_id = 1 AND user_id = 20') - >find();
find獲取一條數(shù)據(jù),find(1)獲取id為1的數(shù)據(jù),find(2)獲取id為2的數(shù)據(jù)。最后一個(gè)是獲取條件為where的中的第一條數(shù)據(jù)。
5.select()
$model = M('WxuserCollection'); $res = $model - >where('good_id=1105 AND store_id = 1 AND user_id = 20') - >field('id,good_id as good') - >select();
獲取所有數(shù)據(jù)。這里的好處就是,不用考慮sql語(yǔ)句的順序了,隨心所欲調(diào)用函數(shù)就可以了。
6.delete()
$model = M('WxuserCollection'); $res = $model - >where('id=1') - >delete(); // 成功返回1 失敗返回0
根據(jù)條件進(jìn)行刪除操作
7.field()
$model = M('WxuserCollection'); $res = $model - >field('id,good_id as good') - >select(); $res = $model - >field(array('id', 'good_id' = >'good')) - >select(); $res = $model - >field('id', true) - >select();
字符串,數(shù)組兩種方式,第三個(gè)是表示獲取處理id之外的所有字段。
8.order()
$model = M('WxuserCollection'); $res = $model - >order('id desc') - >select(); $res = $model - >order('id asc') - >select(); $res = $model - >order(array('id' = >'desc')) - >select(); $res = $model - >order(array('id')) - >select();
字符串,數(shù)組兩種方式,默認(rèn)asc。
9.join()
$Model->join(' work ON artist.id = work.artist_id')->join('card ON artist.card_id = card.id')->select(); $Model->join('RIGHT JOIN work ON artist.id = work.artist_id')->select(); $Model->join(array(' work ON artist.id = work.artist_id','card ON artist.card_id = card.id'))->select();
默認(rèn)采用LEFT JOIN 方式,如果需要用其他的JOIN方式,可以改成第二種,
如果join方法的參數(shù)用數(shù)組的話,只能使用一次join方法,并且不能和字符串方式混合使用。
10.setInc()
$User = M("User"); // 實(shí)例化User對(duì)象 $User->where('id=5')->setInc('score',3); // 用戶的積分加3 $User->where('id=5')->setInc('score'); // 用戶的積分加1 $User->where('id=5')->setDec('score',5); // 用戶的積分減5 $User->where('id=5')->setDec('score'); // 用戶的積分減1
11.getField()
獲取某個(gè)字段值
$User = M("User"); // 實(shí)例化User對(duì)象 // 獲取ID為3的用戶的昵稱 $nickname = $User->where('id=3')->getField('nickname');
返回的nickname是一個(gè)字符串結(jié)果。也就是說(shuō),即使有滿足條件的多個(gè)字段,也只會(huì)返回一個(gè)結(jié)果。
獲取某個(gè)字段列
如果希望返回符合要求的字段列(多個(gè)結(jié)果),可以使用:
$User = M("User"); // 實(shí)例化User對(duì)象 // 獲取status為1的用戶的昵稱列表 $nickname = $User->where('status=1')->getField('nickname',true);
第二個(gè)參數(shù)傳入了true,返回的nickname則是一個(gè)數(shù)組,包含了所有滿足條件的昵稱列表。
如果需要限制返回結(jié)果數(shù)量,可以使用:
$nickname = $User->where('status=1')->getField('nickname',8);
獲取2個(gè)字段列表
$User = M("User"); // 實(shí)例化User對(duì)象 // 獲取status為1的用戶的昵稱列表 $nickname = $User->where('status=1')->getField('id,nickname');
如果getField方法傳入多個(gè)字段名稱的話,默認(rèn)返回一個(gè)關(guān)聯(lián)數(shù)組,以第一個(gè)字段的值為索引(所以第一個(gè)字段要盡量選擇不會(huì)重復(fù)的)。
獲取多個(gè)字段列表
$result = $User->where('status=1')->getField('id,account,nickname');
如果傳入了2個(gè)以上的字段名,則返回一個(gè)二維數(shù)組(類似select方法的返回值,區(qū)別在于索引是二維數(shù)組的鍵名是第一個(gè)字段的值)
綜合使用案例
$where = array('a.store_id' => $this->store_id, 'a.user_id' => $this->user_id); $collects = $this->collectModel->table("sh_wxuser_collection a")->field(array('b.name','b.price','b.oprice','b.logoimg','a.goods_id'))->limit($start, $offset)->order('a.addtime DESC')->where($where)->join(' sh_goods b ON a.goods_id = b.id')->select();// 獲取當(dāng)前頁(yè)的記錄 echo M()->getLastSql(); // 調(diào)試sql語(yǔ)句用 $count = $this->collectModel->table("sh_wxuser_collection a")->where($where)->count(); // 獲取總的記錄數(shù)
這里由于結(jié)合了兩張表,所以用到了table方法,重新定義表名,相應(yīng)的條件和參數(shù)都要加上前綴。a. 或者b.
其中field字段要么是一個(gè)字符串,要么是數(shù)組。
field('b.name', 'b.price', 'b.oprice', 'b.logoimg', 'a.goods_id') // 錯(cuò)誤
我之前就這么寫(xiě),問(wèn)題大大的。
使用框架,就不能靈活的寫(xiě)sql了。不過(guò)對(duì)sql有一個(gè)深刻的認(rèn)識(shí),也有利于靈活的使用好框架。
用于調(diào)試sql語(yǔ)句的方法。
echo M()->getLastSql();
很方便。
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》、《smarty模板入門基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
- tp5(thinkPHP5)框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)查詢的方法
- thinkPHP5框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)連接,跨數(shù)據(jù)連接查詢操作示例
- thinkPHP5實(shí)現(xiàn)的查詢數(shù)據(jù)庫(kù)并返回json數(shù)據(jù)實(shí)例
- ThinkPHP5.1框架數(shù)據(jù)庫(kù)鏈接和增刪改查操作示例
- tp5(thinkPHP5)框架數(shù)據(jù)庫(kù)Db增刪改查常見(jiàn)操作總結(jié)
- thinkphp3.2.3版本的數(shù)據(jù)庫(kù)增刪改查實(shí)現(xiàn)代碼
- Thinkphp使用mongodb數(shù)據(jù)庫(kù)實(shí)現(xiàn)多條件查詢方法
- thinkphp3查詢mssql數(shù)據(jù)庫(kù)亂碼解決方法分享
- ThinkPHP實(shí)現(xiàn)轉(zhuǎn)換數(shù)據(jù)庫(kù)查詢結(jié)果數(shù)據(jù)到對(duì)應(yīng)類型的方法
- tp5.1 框架數(shù)據(jù)庫(kù)高級(jí)查詢技巧實(shí)例總結(jié)
相關(guān)文章
老生常談PHP面向?qū)ο笾畼?biāo)識(shí)映射
下面小編就為大家?guī)?lái)一篇老生常談PHP面向?qū)ο笾畼?biāo)識(shí)映射。小編覺(jué)得挺不錯(cuò)的?,F(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06PHP編程開(kāi)發(fā)怎么提高編程效率 提高PHP編程技術(shù)
這篇文章主要介紹了PHP編程開(kāi)發(fā)怎么提高編程效率(牢記這20個(gè)點(diǎn))的相關(guān)資料,需要的朋友可以參考下2015-11-11thinkphp3.2實(shí)現(xiàn)上傳圖片的控制器方法
這篇文章主要介紹了thinkphp3.2實(shí)現(xiàn)上傳圖片的控制器方法,結(jié)合實(shí)例形式分析了thinkPHP圖片文件上傳相關(guān)的文件類型判斷,文件路徑及相關(guān)屬性操作技巧,需要的朋友可以參考下2016-04-04分享PHP函數(shù)實(shí)現(xiàn)數(shù)字與文字分頁(yè)代碼
這篇文章主要介紹了PHP函數(shù)實(shí)現(xiàn)數(shù)字與文字分頁(yè)代碼,定義分頁(yè)函數(shù)_paging,在核心函數(shù)庫(kù)包裝代碼,需要的朋友可以參考下2015-07-07PHP命名空間(namespace)的動(dòng)態(tài)訪問(wèn)及使用技巧
上篇文章介紹了PHP命名空間的一些術(shù)語(yǔ),其解析規(guī)則,本文我們來(lái)繼續(xù)講述PHP命名空間動(dòng)態(tài)訪問(wèn)及使用技巧,希望能有所幫助2014-08-08laravel 實(shí)現(xiàn)用戶登錄注銷并限制功能
今天小編就為大家分享一篇laravel 實(shí)現(xiàn)用戶登錄注銷并限制功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10