thinkPHP數(shù)據(jù)庫增刪改查操作方法實例詳解
本文實例講述了thinkPHP數(shù)據(jù)庫增刪改查操作方法。分享給大家供大家參考,具體如下:
thinkphp對數(shù)據(jù)庫增刪改查進行了封裝操作,使得使用更加方便,但是不一定靈活。
可以用封裝的用,需要寫sql,可以執(zhí)行sql。
1.原始的
$Model = new Model(); // 實例化一個model對象 沒有對應(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.針對表實例化的,這里的表原名是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);
另一種寫法,_可以寫成大寫,它會自動轉(zhuǎn)化成_
$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獲取一條數(shù)據(jù),find(1)獲取id為1的數(shù)據(jù),find(2)獲取id為2的數(shù)據(jù)。最后一個是獲取條件為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ù)條件進行刪除操作
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ù)組兩種方式,第三個是表示獲取處理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ù)組兩種方式,默認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方法的參數(shù)用數(shù)組的話,只能使用一次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是一個字符串結(jié)果。也就是說,即使有滿足條件的多個字段,也只會返回一個結(jié)果。
獲取某個字段列
如果希望返回符合要求的字段列(多個結(jié)果),可以使用:
$User = M("User"); // 實例化User對象 // 獲取status為1的用戶的昵稱列表 $nickname = $User->where('status=1')->getField('nickname',true);
第二個參數(shù)傳入了true,返回的nickname則是一個數(shù)組,包含了所有滿足條件的昵稱列表。
如果需要限制返回結(jié)果數(shù)量,可以使用:
$nickname = $User->where('status=1')->getField('nickname',8);
獲取2個字段列表
$User = M("User"); // 實例化User對象 // 獲取status為1的用戶的昵稱列表 $nickname = $User->where('status=1')->getField('id,nickname');
如果getField方法傳入多個字段名稱的話,默認返回一個關(guān)聯(lián)數(shù)組,以第一個字段的值為索引(所以第一個字段要盡量選擇不會重復的)。
獲取多個字段列表
$result = $User->where('status=1')->getField('id,account,nickname');
如果傳入了2個以上的字段名,則返回一個二維數(shù)組(類似select方法的返回值,區(qū)別在于索引是二維數(shù)組的鍵名是第一個字段的值)
綜合使用案例
$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(); // 調(diào)試sql語句用 $count = $this->collectModel->table("sh_wxuser_collection a")->where($where)->count(); // 獲取總的記錄數(shù)
這里由于結(jié)合了兩張表,所以用到了table方法,重新定義表名,相應(yīng)的條件和參數(shù)都要加上前綴。a. 或者b.
其中field字段要么是一個字符串,要么是數(shù)組。
field('b.name', 'b.price', 'b.oprice', 'b.logoimg', 'a.goods_id') // 錯誤
我之前就這么寫,問題大大的。
使用框架,就不能靈活的寫sql了。不過對sql有一個深刻的認識,也有利于靈活的使用好框架。
用于調(diào)試sql語句的方法。
echo M()->getLastSql();
很方便。
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》、《smarty模板入門基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
- tp5(thinkPHP5)框架實現(xiàn)多數(shù)據(jù)庫查詢的方法
- thinkPHP5框架實現(xiàn)多數(shù)據(jù)庫連接,跨數(shù)據(jù)連接查詢操作示例
- thinkPHP5實現(xiàn)的查詢數(shù)據(jù)庫并返回json數(shù)據(jù)實例
- ThinkPHP5.1框架數(shù)據(jù)庫鏈接和增刪改查操作示例
- tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結(jié)
- thinkphp3.2.3版本的數(shù)據(jù)庫增刪改查實現(xiàn)代碼
- Thinkphp使用mongodb數(shù)據(jù)庫實現(xiàn)多條件查詢方法
- thinkphp3查詢mssql數(shù)據(jù)庫亂碼解決方法分享
- ThinkPHP實現(xiàn)轉(zhuǎn)換數(shù)據(jù)庫查詢結(jié)果數(shù)據(jù)到對應(yīng)類型的方法
- tp5.1 框架數(shù)據(jù)庫高級查詢技巧實例總結(jié)
相關(guān)文章
PHP編程開發(fā)怎么提高編程效率 提高PHP編程技術(shù)
這篇文章主要介紹了PHP編程開發(fā)怎么提高編程效率(牢記這20個點)的相關(guān)資料,需要的朋友可以參考下2015-11-11分享PHP函數(shù)實現(xiàn)數(shù)字與文字分頁代碼
這篇文章主要介紹了PHP函數(shù)實現(xiàn)數(shù)字與文字分頁代碼,定義分頁函數(shù)_paging,在核心函數(shù)庫包裝代碼,需要的朋友可以參考下2015-07-07PHP命名空間(namespace)的動態(tài)訪問及使用技巧
上篇文章介紹了PHP命名空間的一些術(shù)語,其解析規(guī)則,本文我們來繼續(xù)講述PHP命名空間動態(tài)訪問及使用技巧,希望能有所幫助2014-08-08