2-7 鏈式操作
2018-05-07
鏈式操作:
<?php namespace?app\index\controller; use?think\controller; use?think\Db; class?Index { ????public?function?index(){ ????????$db?=?Db::name('user'); //????????$data?=?[]; //????????for?($i=1;?$i<21;?$i++){ //????????????$data[]?=?[ //????????????????'username'?=>?"wenco_{$i}", //????????????????'password'?=>?md5("wenco_{$i}"), //????????????????'email'????=>?"wenco_{$i}@qq.com", //????????????????'num'??????=>?$i+100 //????????????]; //????????} //????????$res?=?$db->insertALL($data); ????????$res?=Db::table('imooc_user') ????????????->field("username","id","group") ????????????->order('id',DESC) ????????????->limit(3,5)//limit分頁limit((m-1)*n,n)===??m:當前頁數(shù),n:每頁多少條數(shù)據(jù) ????????????->page(2,5) ????????????->group("'group'") ????????????->select(); ????????dump($res); ????} }
0
采集 0
2-6 條件構造器
2018-05-07
條件構造:
<?php namespace?app\index\controller; use?think\controller; use?think\Db; class?Index { ????public?function?index(){ ??????????$db?=?Db::name('user'); ??????????#buildSql:返回sql語句 ??????????#不區(qū)分大小寫 ??????????#EQ???= ??????????#NEQ??<> ??????????#LT???< ??????????#ELT???<= ??????????#GT???> ??????????#EGT???>= ??????????#BETWEEN???BETWEEN?*?AND?* ??????????#NOTBETWEEN????NOTBETWEEN?*?AND?* ??????????#IN???IN?(*,*) ??????????#NOTIN???NOT?IN(*,*) ??????????#連續(xù)使用where方法構成and?where條件, //????????????$sql?=?$db->where('id','BETWEEN'?,'1,5')->buildSql(); ????????????$sql?=?$db ????????????????->where('id','exp'?,'not?in(1,2,3)') ????????????????->where('username','eq',?'wenco_1') ????????????????->buildSql(); ??????????#where與whereOr?構成or的關系 ????????????$sql?=?$db ????????????????->where('id','exp'?,'not?in(1,2,3)') ????????????????->whereOr('username','eq',?'wenco_1') ????????????????->buildSql(); ??????????dump($sql); ????} }
0
采集 0
2-5 數(shù)據(jù)庫刪除操作
2018-05-07
delete:
<?php namespace?app\index\controller; use?think\controller; use?think\Db; class?Index { ????public?function?index(){ ??????????$db?=?Db::name('user'); ??????????#delete返回影響行數(shù),當在deleted()中的參數(shù)為主鍵時可不加where條件 ??????????$res?=?$db->delete(2); ??????????dump($res); ????} }
0
采集 0
2-4 數(shù)據(jù)庫更新操作
2018-05-07
數(shù)據(jù)更新update/setField/setInc/setDec
<?php namespace?app\index\controller; use?think\controller; use?think\Db; class?Index { ????public?function?index() ????{ ??????????$db?=?Db::name('user'); //??????#update返回影響行數(shù) //????????$res?=?$db->where(['id'?=>?1])->update(['username'?=>?'1234567']); ????????#setField,設置字段的值,返回被影響的數(shù)據(jù)的行數(shù) //????????$res?=?$db->where(['id'=>3])->setField([ //????????????'username'?=>?'88888888', //????????????'email'????=>?'66666666' //????????]); ????????#setInc返回影響行數(shù),第一個參數(shù)為字段名,第二個數(shù)據(jù)為在原數(shù)據(jù)的基礎上的/*自增數(shù)*/ ????????$res?=?$db->where(['id'=>'1'])->setInc('num',5); ????????#setDec同setInc,只不過第二個參數(shù)為/*自減數(shù)*/ ????????$res?=?$db->where(['id'=>'1'])->setDec('num',5); ????????dump($res); ????} }
0
采集 0
2-3 添加數(shù)據(jù)
2018-05-07
數(shù)據(jù)插入:insert /insertGetId/insertAll
<?php namespace?app\index\controller; use?think\controller; use?think\Db; class?Index { ????public?function?index(){ ????????$db?=?Db::name('user'); ????????#insert?返回值為插入影響行數(shù) //????????$res?=?$db->insert([ //???????????'username'????=>?'wenco', //????????????'email'??????=>?'wenco@qq.com', //????????????'password'???=>?md5('today') //????????]); ????????#inset插入并且返回自增ID //????????$res?=?$db->insertGetId([ //???????????'username'????=>?'wenco', //????????????'email'??????=>?'wenco@qq.com', //????????????'password'???=>?md5('today') //????????]); ????????#insetALL,返回插入成功數(shù) ????????$data?=?[]; ????????for?($i?=?1;?$i?<?10;$i++){ ????????????$data[]?=?['username'????=>?"wenco_{$i}", ????????????'email'??????=>?"wenco_{$i}@qq.com", ????????????'password'???=>?md5("today_{$i}") ????????????]; ????????} ????????$res?=?$db->insertAll($data); ????????dump($res); ????} }
0
采集 0
2-2 數(shù)據(jù)庫查詢操作
2018-05-07
thinkphp Db類提供的四個查找數(shù)據(jù)的方法;
select /column /find/ value
<?php namespace?app\index\controller; use?think\controller; use?think\Db; class?Index { ????public?function?index(){ #?使用sql語句的方式查詢數(shù)據(jù)庫 //????????$res?=?Db::query("select?*?from?imooc_user?where?id=?",[1]); #?返回修改影響的結果條數(shù) /*???????$res?=?Db::execute("insert?into?imooc_user?set?username?=?,password=?,email=?",?[ ???????????'imooc', ???????????md5('imooc'), ???????????'imooc@qq.com' ????????]);*/ #返回一個二維數(shù)組,在不添加where條件下輸出所有數(shù)據(jù),如果結果不存在返回一個空數(shù)組 ????????/*$res?=?Db::table('imooc_user')->?where([ ????????????'id'?=>?"1" ????????])->select();*/ #一維數(shù)組,返回該列下的所有值,如果存在第二個參數(shù),那個以第二個參數(shù)的值作為數(shù)組的key值,如果結果不存在,返回一個空數(shù)組 ????????//$res?=?Db::table('imooc_user')->column('username','email'); #find方法,返回一個一維數(shù)組,返回一條數(shù)據(jù),如不添加where條件默認返回正序排列下id最小的那一條記錄,如果結果不存在返回null /*????????$res?=?Db::table('imooc_user')->where([ ????????????'id'?=>?"2" ????????])->find();*/ #返回一條記錄,某個字段的值,當結果不存在返回null //????????$res?=?Db::table('imooc_user')->where(['id'=>'2'])->value('username'); #用Db::name,相當于Db::table傳遞表前綴 //????????$res?=?Db::name('user')->select(); #助手函數(shù)db,與db類不同的是助手函數(shù)每次調用都要進行實例化! ????????$res?=?db('user')->select(); ????????dump($res); ????} }
0
采集 0
2-1 數(shù)據(jù)庫的連接操作
2018-05-06
修改數(shù)據(jù)庫配置方式
1.可在config下的database.php返回配置信息,直接 DB::connect()
2.在方法里面引入 use think\db;DB::connect(array);數(shù)組即數(shù)據(jù)庫連接配置
3.同樣是DB::connect("mysql://root:password@127.0.0.1:3306:/databasename#utf8");//use think\Db?
4.在配置文件中,添加一個數(shù)組,DB::connect("數(shù)組鍵名");//use think\Db
1
采集 0
舉報
0/150
提交
取消