Mysql,SqlServer,pgSQL,Sqlite等數(shù)據(jù)庫的支持
二、如何連接數(shù)據(jù)庫1.配置文件定義a.配置文件目錄
項目/application/database.php
b.如何配置
return [ // 數(shù)據(jù)庫類型 type = mysql , // 服務(wù)器地址 hostname = 127.0.0.1 , // 數(shù)據(jù)庫名 database = , // 用戶名 username = root , // 密碼 password = , // 端口 hostport = 3306 ,];
c.如何使用
// 實例化系統(tǒng)數(shù)據(jù)庫類$DB=new Db;// 查詢數(shù)據(jù)$data=$DB::table( user )- select();//使用sql語句$data=$DB::query( select * from user2.方法配置
1.使用數(shù)組
//Db類中的connect方法:數(shù)據(jù)庫初始化 并取得數(shù)據(jù)庫類實例 $DB=Db::connect([ // 數(shù)據(jù)庫類型 type = mysql , // 服務(wù)器地址 hostname = 127.0.0.1 , // 數(shù)據(jù)庫名 database = edu , // 用戶名 username = root , // 密碼 password = , // 端口 hostport = 3306 ,]);
2.使用字符串
//Db類中的connect方法:數(shù)據(jù)庫初始化 并取得數(shù)據(jù)庫類實例$DB=Db::connect( mysql://root:@127.0.0.1:3306/edu#utf8
3.如何使用
$data=$DB- table( user )- select();3.模型類定義
1.創(chuàng)建數(shù)據(jù)模型
a.命令行創(chuàng)建
b.手動創(chuàng)建
1.打開數(shù)據(jù)模型目錄(項目/application/index/model)
2.在目錄文件下新建文件User.php
3.在文件中書寫代碼
?php // 聲明命名空間(位置) namespace app/index/model; // 聲明控制器 use think/Model; html' target='_blank'>class User extends Model }?
2.如何設(shè)置
?php // 聲明命名空間(位置) namespace app/index/model; // 聲明控制器 use think/Model; class User extends Model // 使用數(shù)組連接數(shù)據(jù)庫 protected $connection=[ // 數(shù)據(jù)庫類型 type = mysql , // 服務(wù)器地址 hostname = 127.0.0.1 , // 數(shù)據(jù)庫名 database = edu , // 用戶名 username = root , // 密碼 password = , // 端口 hostport = 3306 , ]; //使用字符串 protected $connection= mysql://root:@127.0.0.1:3306/edu#utf8 }?
3.如何控制器中使用
// 使用模型定義連接public function data2(){ echo 使用模型連接數(shù)據(jù)庫 $user=new /app/index/model/User(); dump($user::all());}三、查詢數(shù)據(jù)1.tp方法
// 實例化系統(tǒng)數(shù)據(jù)庫類$DB=new Db;// 查詢數(shù)據(jù)$data=$DB::table( user )- select();2.使用sql語句
//使用sql語句$data=$DB::query( select * from user四、數(shù)據(jù)庫的基本使用
支持query(查詢操作)和execute(寫入操作)
0.獲取指定sql語句// 獲取執(zhí)行的sql語句echo Db::getLastSql();1.查詢
$data=Db::query( select * from user $data=Db::query( select * from user where id =? and id =? ,[5,8]);2.增加
$data=Db::execute( insert into user value(null, user1 , 123 , 18 ) $data=Db::execute( insert into user value(null,?,?,?) ,[ user2 , 123 , 20 ] $data=Db::execute( insert into user value(null,:name,:pass,:age) ,[ name = user3 , pass = 123 , age = 203.刪除
$data=Db::execute( delete from user where id=10 $data=Db::execute( delete from user where id ? ,[15]);$data=Db::execute( delete from user where id :id ,[ id = 10]);4.修改
$data=Db::execute( update user set age= 20 where id=? ,[15]);五、TP數(shù)據(jù)處理1.查詢操作
1.table方法查詢數(shù)據(jù) // 查詢所有數(shù)據(jù) $data=Db::table( user )- select(); // 查詢一條數(shù)據(jù) $data=Db::table( user )- find();2.name方法查詢數(shù)據(jù) //name方法會自動添加上配置文件中的表前綴,與配置文件有關(guān) $data=Db::name( user )- select(); $data=Db::name( user )- find();3.助手函數(shù) $data=db( user )- select(); $data=db( user )- find();4.where條件匹配 $data=Db::table( user )- where( id , ,5)- select(); $data=Db::table( user )- where( id , ,11)- where( id , ,8)- select(); $data=Db::table( user )- where( name , like , %tian% )- select(); $data=Db::table( user )- where( name , wanlisha )- where( pass , wanlisha )- select();5.whereor條件查詢 $data=Db::table( user )- where( id , = , 21 )- whereOr( id , = ,5)- select(); $data=Db::table( user )- where( name , like , %tian% )- whereOr( name , like , %wanli% )- select(); $data=Db::table( user )- where( name|pass , like , %tian% )- select();//6.limit截取數(shù)據(jù) $data=Db::table( user )- limit(2)- select(); $data=Db::table( user )- limit(0,2)- select();7.order實現(xiàn)排序 $data=Db::table( user )- order( id )- select(); $data=Db::table( user )- order( id , desc )- select();8.field 設(shè)置查詢字段 //設(shè)置查詢字段 $data=Db::table( user )- field( name,pass )- select(); $data=Db::table( user )- field([ name , pass ])- select(); // 給name起別名 $data=Db::table( user )- field( name uname,pass )- select(); $data=Db::table( user )- field([ name = uname , pass ])- select(); // sql的系統(tǒng)函數(shù) $data=Db::table( user )- field( count(*) as tot )- select(); $data=Db::table( user )- field([ count(*) = tot ])- select(); //排除字段 $data=Db::table( user )- field( name,pass ,true)- select(); $data=Db::table( user )- field([ name , pass ],true)- select();9.Page實現(xiàn)分頁效果 $data=Db::table( user )- page(3,5)- select(); $data=Db::table( user )- page( 3,5 )- select();10.分組聚合 $data=Db::table( user )- field( pass,count(*) tot )- group( pass )- select();11.having過濾 // 只能結(jié)合分組使用 $data=Db::table( user )- field( pass,count(*) tot )- having( tot =4 )- group( pass )- select();12.多表查詢 // 內(nèi)斂實現(xiàn)數(shù)據(jù)庫連接 $data=Db::query( select product.*,fenlei.name tname from fenlei,product where product.cid=fenlei.id $data=Db::table( product )- field( product.*,fenlei.name tname )- join( fenlei , product.cid=fenlei.id )- select(); // 右鏈接 $data=Db::table( product )- field( product.*,fenlei.name tname )- join( fenlei , product.cid=fenlei.id , right )- select(); // 左鏈接 $data=Db::table( product )- field( product.*,fenlei.name tname )- join( fenlei , product.cid=fenlei.id , left )- select();13.別名使用-給表起別名 $data=Db::table( product )- alias( p )- field( p.*,f.name fname )- join( fenlei f , p.cid=f.id , left )- select();14.union集合 $data=Db::field( name )- table( user )- union( select name from product )- select();15.參數(shù)綁定bind為了防止sql注入 //自動輕微防止sql注入 $data=Db::table( user )- where( id ,$id)- delete(); //不防注入 建議不要使用原生的sql語句 $data=Db::execute( delete from user where id=$id //防注入 $data=Db::table( user )- where( id , :id )- bind([ id = [$id,/PDO::PARAM_INT]])- delete();16.統(tǒng)計數(shù)據(jù) $data=Db::table( user )- max( age $data=Db::table( user )- min( age $data=Db::table( user )- avg( age $data=Db::table( user )- sum( age $data=Db::table( user )- count();17.視圖查詢(多表查詢) $data=Db::view( goods , id,name,price )- view( type , name , type.id=goods.cid )- select(); // 左連接 $data=Db::view( goods , id,name,price )- view( type , name , type.id=goods.cid , right )- select(); // 右連接 $data=Db::view( goods , id,name,price )- view( type , name , type.id=goods.cid , left )- select();2.插入操作
1.插入單條數(shù)據(jù) // 數(shù)組中的字段名必須和數(shù)據(jù)庫中字段名一致 $data=[ name = 張三 , pass = 123 , age = 18 // 返回值:影響行數(shù) $code=Db::table( user )- insert($data); $code=db( user )- insert($data);2.插入多條數(shù)據(jù) $data=[ name = 張三1 , pass = 123 , age = 18 name = 張三2 , pass = 123 , age = 18 //返回值:影響行數(shù) $code=Db::table( user )- insertAll($data); $code=db( user )- insertAll($data);3.獲取最后一次插入的id $data=[ name = 張三1 , pass = 123 , age = 18 $code=Db::table( user )- insertGetId($data); $code=db( user )- insertGetId($data);3.更新數(shù)據(jù)
1.修改數(shù)據(jù) $code=Db::table( user )- where( id , ,5)- update([ age = 111, pass = 111 $code=Db::table( user )- update([ id = 5, age = 60]); code=Db::table( user )- where( id ,5)- setField( age ,111);2.設(shè)置自增 $code=Db::table( user )- where( id ,6)- setInc( age 3.設(shè)置自減 $code=Db::table( user )- where( id ,7)- setDec( age $code=Db::table( user )- where( id ,5)- setDec( age ,3);4.刪除數(shù)據(jù)
1.刪除一條數(shù)據(jù) $code=Db::table( user )- where( id ,6)- delete(); $code=Db::table( user )- delete(7);2.刪除多條數(shù)據(jù) $code=Db::table( user )- where( id in(1,2) )- delete(); $code=Db::table( user )- delete([2,3]);3.刪除區(qū)間數(shù)據(jù) $code=Db::table( user )- where( id 0 and id 5 )- delete();六、事務(wù)機制1.mysql事務(wù)
要求數(shù)據(jù)的引擎必須是InnoDB
重點:對要操作的數(shù)據(jù)表執(zhí)行語句:ALTER TABLE user ENGINE=INNODB;
1. 自動控制事務(wù)
Db::transaction(function(){ // 刪除一條數(shù)據(jù) Db::table( user )- delete(11); Db::table( user )- deletes(40);});2. 手動控制事務(wù)
// 手動控制事務(wù) // 開啟事務(wù) Db::startTrans(); try{ // 刪除數(shù)據(jù) $a=Db::table( user )- delete(11); // 判斷是否刪除成功 if(!$a){ throw new /Exception( 刪除11沒有成功 } // 刪除不存在的數(shù)據(jù) $b=Db::table( user )- delete(12); if(!$b){ throw new /Exception( 刪除12沒有成功 } // 執(zhí)行提交操作 Db::commit();`這里寫代碼片` echo 成功 }catch(/Exception $e){ // 回滾事務(wù) echo 失敗 Db::rollback(); dump($e- getmessage()); } // 開啟事務(wù) Db::startTrans(); // 刪除數(shù)據(jù) $a=Db::table( user )- delete(1); $b=Db::table( user )- delete(2); // 判斷條件 if($a $b){ // 提交事務(wù) Db::commit(); }else{ Db::rollback(); }一、支持?jǐn)?shù)據(jù)庫的類型Mysql,SqlServer,pgSQL,Sqlite等數(shù)據(jù)庫的支持
二、如何連接數(shù)據(jù)庫1.配置文件定義a.配置文件目錄
項目/application/database.php
b.如何配置
return [ // 數(shù)據(jù)庫類型 type = mysql , // 服務(wù)器地址 hostname = 127.0.0.1 , // 數(shù)據(jù)庫名 database = , // 用戶名 username = root , // 密碼 password = , // 端口 hostport = 3306 ,];
c.如何使用
// 實例化系統(tǒng)數(shù)據(jù)庫類$DB=new Db;// 查詢數(shù)據(jù)$data=$DB::table( user )- select();//使用sql語句$data=$DB::query( select * from user2.方法配置
1.使用數(shù)組
//Db類中的connect方法:數(shù)據(jù)庫初始化 并取得數(shù)據(jù)庫類實例 $DB=Db::connect([ // 數(shù)據(jù)庫類型 type = mysql , // 服務(wù)器地址 hostname = 127.0.0.1 , // 數(shù)據(jù)庫名 database = edu , // 用戶名 username = root , // 密碼 password = , // 端口 hostport = 3306 ,]);
2.使用字符串
//Db類中的connect方法:數(shù)據(jù)庫初始化 并取得數(shù)據(jù)庫類實例$DB=Db::connect( mysql://root:@127.0.0.1:3306/edu#utf8
3.如何使用
$data=$DB- table( user )- select();3.模型類定義
1.創(chuàng)建數(shù)據(jù)模型
a.命令行創(chuàng)建
b.手動創(chuàng)建
1.打開數(shù)據(jù)模型目錄(項目/application/index/model)
2.在目錄文件下新建文件User.php
3.在文件中書寫代碼
?php // 聲明命名空間(位置) namespace app/index/model; // 聲明控制器 use think/Model; class User extends Model }?
2.如何設(shè)置
?php // 聲明命名空間(位置) namespace app/index/model; // 聲明控制器 use think/Model; class User extends Model // 使用數(shù)組連接數(shù)據(jù)庫 protected $connection=[ // 數(shù)據(jù)庫類型 type = mysql , // 服務(wù)器地址 hostname = 127.0.0.1 , // 數(shù)據(jù)庫名 database = edu , // 用戶名 username = root , // 密碼 password = , // 端口 hostport = 3306 , ]; //使用字符串 protected $connection= mysql://root:@127.0.0.1:3306/edu#utf8 }?
3.如何控制器中使用
// 使用模型定義連接public function data2(){ echo 使用模型連接數(shù)據(jù)庫 $user=new /app/index/model/User(); dump($user::all());}三、查詢數(shù)據(jù)1.tp方法// 實例化系統(tǒng)數(shù)據(jù)庫類$DB=new Db;// 查詢數(shù)據(jù)$data=$DB::table( user )- select();2.使用sql語句
//使用sql語句$data=$DB::query( select * from user四、數(shù)據(jù)庫的基本使用
支持query(查詢操作)和execute(寫入操作)
0.獲取指定sql語句// 獲取執(zhí)行的sql語句echo Db::getLastSql();1.查詢
$data=Db::query( select * from user $data=Db::query( select * from user where id =? and id =? ,[5,8]);2.增加
$data=Db::execute( insert into user value(null, user1 , 123 , 18 ) $data=Db::execute( insert into user value(null,?,?,?) ,[ user2 , 123 , 20 ] $data=Db::execute( insert into user value(null,:name,:pass,:age) ,[ name = user3 , pass = 123 , age = 203.刪除
$data=Db::execute( delete from user where id=10 $data=Db::execute( delete from user where id ? ,[15]);$data=Db::execute( delete from user where id :id ,[ id = 10]);4.修改
$data=Db::execute( update user set age= 20 where id=? ,[15]);五、TP數(shù)據(jù)處理1.查詢操作
1.table方法查詢數(shù)據(jù) // 查詢所有數(shù)據(jù) $data=Db::table( user )- select(); // 查詢一條數(shù)據(jù) $data=Db::table( user )- find();2.name方法查詢數(shù)據(jù) //name方法會自動添加上配置文件中的表前綴,與配置文件有關(guān) $data=Db::name( user )- select(); $data=Db::name( user )- find();3.助手函數(shù) $data=db( user )- select(); $data=db( user )- find();4.where條件匹配 $data=Db::table( user )- where( id , ,5)- select(); $data=Db::table( user )- where( id , ,11)- where( id , ,8)- select(); $data=Db::table( user )- where( name , like , %tian% )- select(); $data=Db::table( user )- where( name , wanlisha )- where( pass , wanlisha )- select();5.whereor條件查詢 $data=Db::table( user )- where( id , = , 21 )- whereOr( id , = ,5)- select(); $data=Db::table( user )- where( name , like , %tian% )- whereOr( name , like , %wanli% )- select(); $data=Db::table( user )- where( name|pass , like , %tian% )- select();//6.limit截取數(shù)據(jù) $data=Db::table( user )- limit(2)- select(); $data=Db::table( user )- limit(0,2)- select();7.order實現(xiàn)排序 $data=Db::table( user )- order( id )- select(); $data=Db::table( user )- order( id , desc )- select();8.field 設(shè)置查詢字段 //設(shè)置查詢字段 $data=Db::table( user )- field( name,pass )- select(); $data=Db::table( user )- field([ name , pass ])- select(); // 給name起別名 $data=Db::table( user )- field( name uname,pass )- select(); $data=Db::table( user )- field([ name = uname , pass ])- select(); // sql的系統(tǒng)函數(shù) $data=Db::table( user )- field( count(*) as tot )- select(); $data=Db::table( user )- field([ count(*) = tot ])- select(); //排除字段 $data=Db::table( user )- field( name,pass ,true)- select(); $data=Db::table( user )- field([ name , pass ],true)- select();9.Page實現(xiàn)分頁效果 $data=Db::table( user )- page(3,5)- select(); $data=Db::table( user )- page( 3,5 )- select();10.分組聚合 $data=Db::table( user )- field( pass,count(*) tot )- group( pass )- select();11.having過濾 // 只能結(jié)合分組使用 $data=Db::table( user )- field( pass,count(*) tot )- having( tot =4 )- group( pass )- select();12.多表查詢 // 內(nèi)斂實現(xiàn)數(shù)據(jù)庫連接 $data=Db::query( select product.*,fenlei.name tname from fenlei,product where product.cid=fenlei.id $data=Db::table( product )- field( product.*,fenlei.name tname )- join( fenlei , product.cid=fenlei.id )- select(); // 右鏈接 $data=Db::table( product )- field( product.*,fenlei.name tname )- join( fenlei , product.cid=fenlei.id , right )- select(); // 左鏈接 $data=Db::table( product )- field( product.*,fenlei.name tname )- join( fenlei , product.cid=fenlei.id , left )- select();13.別名使用-給表起別名 $data=Db::table( product )- alias( p )- field( p.*,f.name fname )- join( fenlei f , p.cid=f.id , left )- select();14.union集合 $data=Db::field( name )- table( user )- union( select name from product )- select();15.參數(shù)綁定bind為了防止sql注入 //自動輕微防止sql注入 $data=Db::table( user )- where( id ,$id)- delete(); //不防注入 建議不要使用原生的sql語句 $data=Db::execute( delete from user where id=$id //防注入 $data=Db::table( user )- where( id , :id )- bind([ id = [$id,/PDO::PARAM_INT]])- delete();16.統(tǒng)計數(shù)據(jù) $data=Db::table( user )- max( age $data=Db::table( user )- min( age $data=Db::table( user )- avg( age $data=Db::table( user )- sum( age $data=Db::table( user )- count();17.視圖查詢(多表查詢) $data=Db::view( goods , id,name,price )- view( type , name , type.id=goods.cid )- select(); // 左連接 $data=Db::view( goods , id,name,price )- view( type , name , type.id=goods.cid , right )- select(); // 右連接 $data=Db::view( goods , id,name,price )- view( type , name , type.id=goods.cid , left )- select();2.插入操作
1.插入單條數(shù)據(jù) // 數(shù)組中的字段名必須和數(shù)據(jù)庫中字段名一致 $data=[ name = 張三 , pass = 123 , age = 18 // 返回值:影響行數(shù) $code=Db::table( user )- insert($data); $code=db( user )- insert($data);2.插入多條數(shù)據(jù) $data=[ name = 張三1 , pass = 123 , age = 18 name = 張三2 , pass = 123 , age = 18 //返回值:影響行數(shù) $code=Db::table( user )- insertAll($data); $code=db( user )- insertAll($data);3.獲取最后一次插入的id $data=[ name = 張三1 , pass = 123 , age = 18 $code=Db::table( user )- insertGetId($data); $code=db( user )- insertGetId($data);3.更新數(shù)據(jù)
1.修改數(shù)據(jù) $code=Db::table( user )- where( id , ,5)- update([ age = 111, pass = 111 $code=Db::table( user )- update([ id = 5, age = 60]); code=Db::table( user )- where( id ,5)- setField( age ,111);2.設(shè)置自增 $code=Db::table( user )- where( id ,6)- setInc( age 3.設(shè)置自減 $code=Db::table( user )- where( id ,7)- setDec( age $code=Db::table( user )- where( id ,5)- setDec( age ,3);4.刪除數(shù)據(jù)
1.刪除一條數(shù)據(jù) $code=Db::table( user )- where( id ,6)- delete(); $code=Db::table( user )- delete(7);2.刪除多條數(shù)據(jù) $code=Db::table( user )- where( id in(1,2) )- delete(); $code=Db::table( user )- delete([2,3]);3.刪除區(qū)間數(shù)據(jù) $code=Db::table( user )- where( id 0 and id 5 )- delete();六、事務(wù)機制1.mysql事務(wù)
要求數(shù)據(jù)的引擎必須是InnoDB
重點:對要操作的數(shù)據(jù)表執(zhí)行語句:ALTER TABLE user ENGINE=INNODB;
1. 自動控制事務(wù)
Db::transaction(function(){ // 刪除一條數(shù)據(jù) Db::table( user )- delete(11); Db::table( user )- deletes(40);});2. 手動控制事務(wù)
// 手動控制事務(wù) // 開啟事務(wù) Db::startTrans(); try{ // 刪除數(shù)據(jù) $a=Db::table( user )- delete(11); // 判斷是否刪除成功 if(!$a){ throw new /Exception( 刪除11沒有成功 } // 刪除不存在的數(shù)據(jù) $b=Db::table( user )- delete(12); if(!$b){ throw new /Exception( 刪除12沒有成功 } // 執(zhí)行提交操作 Db::commit();`這里寫代碼片` echo 成功 }catch(/Exception $e){ // 回滾事務(wù) echo 失敗 Db::rollback(); dump($e- getmessage()); } // 開啟事務(wù) Db::startTrans(); // 刪除數(shù)據(jù) $a=Db::table( user )- delete(1); $b=Db::table( user )- delete(2); // 判斷條件 if($a $b){ // 提交事務(wù) Db::commit(); }else{ Db::rollback(); }本文講解了關(guān)于thinkphp5.0數(shù)據(jù)庫操作的案例,更多相關(guān)內(nèi)容請關(guān)注php 。
相關(guān)推薦:
列舉ThinkPHP5與ThinkPHP3的一些異同點
創(chuàng)建一個最簡單的ThinkPhp項目工程
關(guān)于ThinkPHP的增、刪、改、查 的一些總結(jié)
以上就是關(guān)于thinkphp5.0數(shù)據(jù)庫操作的案例的詳細(xì)內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。
新聞熱點
疑難解答
圖片精選