這篇文章主要介紹了thinkPHP資料庫增刪改查操作方法,結合執行個體形式詳細分析了thinkPHP常用資料庫操作函數與相關提示,需要的朋友可以參考下
本文執行個體講述了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();
很方便。
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!