日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

thinkPHP增删改查的方法案例

發布時間:2024/4/14 php 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 thinkPHP增删改查的方法案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

thinkphp對數據庫增刪改查進行了封裝操作,使得使用更加方便,但是不一定靈活。

可以用封裝的用,需要寫sql,可以執行sql。

1.原始的

$Model = new Model(); // 實例化一個model對象 沒有對應任何數據表 $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.針對表實例化的,這里的表原名是sh_wxuser_collection。sh是前綴。

$model = M('wxuser_collection'); //自動省去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);

另一種寫法,_可以寫成大寫,它會自動轉化成_

$model = M('WxuserCollection'); //自動省去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語句

$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語句

$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();

確實挺方便的,但是方便之余,別忘了原始的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獲取一條數據,find(1)獲取id為1的數據,find(2)獲取id為2的數據。最后一個是獲取條件為where的中的第一條數據。

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();

獲取所有數據。這里的好處就是,不用考慮sql語句的順序了,隨心所欲調用函數就可以了。

6.delete()

$model = M('WxuserCollection'); $res = $model - >where('id=1') - >delete(); // 成功返回1 失敗返回0

根據條件進行刪除操作

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();

字符串,數組兩種方式,第三個是表示獲取處理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();

字符串,數組兩種方式,默認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();

默認采用LEFT JOIN 方式,如果需要用其他的JOIN方式,可以改成第二種,

如果join方法的參數用數組的話,只能使用一次join方法,并且不能和字符串方式混合使用。

10.setInc()

$User = M("User"); // 實例化User對象 $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()

獲取某個字段值

$User = M("User"); // 實例化User對象// 獲取ID為3的用戶的昵稱 $nickname = $User->where('id=3')->getField('nickname');

返回的nickname是一個字符串結果。也就是說,即使有滿足條件的多個字段,也只會返回一個結果。

獲取某個字段列

如果希望返回符合要求的字段列(多個結果),可以使用:

$User = M("User"); // 實例化User對象// 獲取status為1的用戶的昵稱列表 $nickname = $User->where('status=1')->getField('nickname',true);

第二個參數傳入了true,返回的nickname則是一個數組,包含了所有滿足條件的昵稱列表。

如果需要限制返回結果數量,可以使用:

$nickname = $User->where('status=1')->getField('nickname',8);

獲取2個字段列表

$User = M("User"); // 實例化User對象// 獲取status為1的用戶的昵稱列表 $nickname = $User->where('status=1')->getField('id,nickname');

如果getField方法傳入多個字段名稱的話,默認返回一個關聯數組,以第一個字段的值為索引(所以第一個字段要盡量選擇不會重復的)。

獲取多個字段列表

$result = $User->where('status=1')->getField('id,account,nickname');

如果傳入了2個以上的字段名,則返回一個二維數組(類似select方法的返回值,區別在于索引是二維數組的鍵名是第一個字段的值)

?

綜合使用案例

?

$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();// 獲取當前頁的記錄 echo M()->getLastSql(); // 調試sql語句用 $count = $this->collectModel->table("sh_wxuser_collection a")->where($where)->count(); // 獲取總的記錄數

?這里由于結合了兩張表,所以用到了table方法,重新定義表名,相應的條件和參數都要加上前綴。a. 或者b.

其中field字段要么是一個字符串,要么是數組。

field('b.name', 'b.price', 'b.oprice', 'b.logoimg', 'a.goods_id') // 錯誤

我之前就這么寫,問題大大的。

使用框架,就不能靈活的寫sql了。不過對sql有一個深刻的認識,也有利于靈活的使用好框架。

用于調試sql語句的方法。

echo M()->getLastSql();?

很方便。

轉載于:https://www.cnblogs.com/jiqing9006/p/4975293.html

總結

以上是生活随笔為你收集整理的thinkPHP增删改查的方法案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。