欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

thinkPHP中如何實(shí)現(xiàn)數(shù)據(jù)庫增刪改查操作-創(chuàng)新互聯(lián)

這篇文章主要介紹了thinkPHP中如何實(shí)現(xiàn)數(shù)據(jù)庫增刪改查操作,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)建站是專業(yè)的龍華網(wǎng)站建設(shè)公司,龍華接單;提供做網(wǎng)站、網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行龍華網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

具體如下:

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

可以用封裝的用,需要寫sql,可以執(zhí)行sql。

1.原始的

$Model = new Model(); // 實(shí)例化一個(gè)model對(duì)象 沒有對(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);

另一種寫法,_可以寫成大寫,它會(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語句

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

確實(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語句的順序了,隨心所欲調(diào)用函數(shù)就可以了。

6.delete()

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

根據(jù)條件進(jìn)行刪除操作

thinkPHP中如何實(shí)現(xiàn)數(shù)據(jù)庫增刪改查操作

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é)果。也就是說,即使有滿足條件的多個(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)前頁的記錄
echo M()->getLastSql(); // 調(diào)試sql語句用
$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ò)誤

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

使用框架,就不能靈活的寫sql了。不過對(duì)sql有一個(gè)深刻的認(rèn)識(shí),也有利于靈活的使用好框架。

用于調(diào)試sql語句的方法。

echo M()->getLastSql();

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“thinkPHP中如何實(shí)現(xiàn)數(shù)據(jù)庫增刪改查操作”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

網(wǎng)頁標(biāo)題:thinkPHP中如何實(shí)現(xiàn)數(shù)據(jù)庫增刪改查操作-創(chuàng)新互聯(lián)
URL標(biāo)題:http://chinadenli.net/article46/hpheg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)網(wǎng)站設(shè)計(jì)微信公眾號(hào)微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名