ThinkPHP5學習(8)資料庫-基本操作
1.資料庫配置
2.query,execute原生態sql語句,增刪改查
3.引數繫結命名佔位符繫結
4.多個數據切換查詢操作
5.查詢構造器
6.DB鏈式操作
7.事務支援
ThinkPHP內建了抽象資料庫訪問層,把不同的資料庫操作封裝起來,我們只需要使用公共的Db類進行操作。
資料庫配置。
application/database.php
每個模組可以設定獨立的資料庫連線引數,admin/database.php
可以採用字串方式,動態定義連線資訊
如database.php中已經定義好,
可以寫:
也可以在模型裡單獨設定資料庫連線資訊
建立一個表:
CREATETABLE `tp_user` (
`id`int(11) NOT NULL,
`name`varchar(45) DEFAULT NULL,
`status`tinyint(1) DEFAULT NULL,
`score`tinyint(2) DEFAULT NULL,
`email`varchar(45) DEFAULT NULL,
PRIMARYKEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
出現:
Class'app\admin\controller\Db' not found
則:
use think\Db;
-
<?php
-
// 資料庫
-
namespace app\admin\controller;
-
use think\Db;
-
class index2
-
{
-
// 查詢資料
-
public function test1(){
-
// 基本使用
-
// 插入記錄
-
Db::execute('insert into tp_user (id,name) values(?,?)',[8,'thinkphp']);
-
// 更新記錄
-
Db::execute('update tp_user set name="thinkphp" where id=1 ');
-
// 查詢資料
-
Db::query('select * from tp_user where id=?',[8]);
-
// 刪除資料
-
Db::query('delete from tp_user where id=5');
-
// 顯示資料列表
-
Db::query('show tables from tp5');
-
// 清空資料表
-
Db::execute('TRUNCATE table tp_user');
-
// 命名佔位符
-
Db::query('select * from tp_user where id=:id',[1]);
-
Db::execute('insert into tp_user (id,name) values(:id,:name)',[8,'thinkphp']);
-
// 查詢構造器
-
// 查詢一個數據
-
Db::table('tp_user')->where('id',1)->find();
-
// 找不到返回null
-
// 查詢資料集
-
Db::table('tp_user')->where('status',1)->select();
-
// 找不到返回空陣列
-
// 設定了表字首時
-
Db::name('user')->where('id',1)->find();
-
Db::name('user')->where('status',1)->select();
-
// find,select 方法之前可以使用所有的鏈式操作方法
-
// db助手函式
-
db('user')->where('id',1)->find();
-
// 使用db助手函式預設每次重新連線資料庫,而使用Db::name,或Db::table都是單例的
-
// 如需要採用相同的連結,可以傳入第三個引數。第二個引數為資料庫的連線引數
-
db('user',[],false)->where('id',1)->find();
-
// 使用query物件或閉包查詢
-
$query = new \think\db\Query();
-
$query->table('tp_user')->where('status',1);
-
Db::find($query);
-
// Db::select($query);
-
Db::select(function($query){
-
$query->table('tp_user')->where('status',1);
-
});
-
// 返回某個欄位值,
-
Db::table('tp_user')->where('id',1)->value('name');
-
// 查詢某一列的值
-
Db::table('tp_user')->where('status',1)->column('name');
-
// 指定索引
-
Db::table('tp_user')->where('status',1)->column('name','id');
-
// 資料集批量處理
-
Db::table('tp_user')->chunk(100,function($users){
-
foreach($users as $user){
-
//
-
echo '1';
-
}
-
});
-
// 或者交給回撥方法myUserIterator處理
-
Db::table('tp_user')->chunk(100,'myUserIterator');
-
// 你可以通過從閉包函式中返回false來中止對資料集的處理
-
Db::table('tp_user')->chunk(100,function($users){
-
// 處理結果集
-
return false;
-
});
-
// 也支援在 chunk 方法之前呼叫其它的查詢方法
-
Db::table('tp_user')->where('score','>',80)->chunk(100,function($users){
-
foreach($users as $user){
-
//
-
}
-
});
-
// JSON型別資料查詢
-
// 查詢JSON型別欄位,info欄位為json型別
-
Db::table('tp_user')->where('info$.email','[email protected]')->find();
-
}
-
// 新增資料
-
public function test2(){
-
// 5版本,新增 data/inc/dec/exp方法設定資料
-
$data = ['foo'=>'bar','bar'=>'foo'];
-
Db::table('tp_user')->insert($data);
-
Db::name('user')->insert($data);
-
// insert 方法新增資料成功返回新增的條數,正常情況返回1
-
Db::name('user')->insert($data);
-
$userId = Db::name('user')->getLastInsID();
-
// 或者直接使用inserGetId方法新增資料並返回主鍵值
-
Db::name('user')->insertGetId($data);
-
// 新增多條資料
-
$data = [
-
['foo'=>'bar','bar'=>'foo'],
-
['foo'=>'bar1','bar'=>'foo1'],
-
['foo'=>'bar2','bar'=>'foo2'],
-
];
-
Db::name('user')->insertAll($data);
-
// 助手函式
-
db('user')->insert($data);
-
db('user')->insertAll($data);
-
// 快捷更新
-
Db::table('user')
-
->data(['name'=>'tp','score'=>90])
-
->insert();
-
}
-
// 更新資料
-
public function test3(){
-
Db::table('tp_user')
-
->where('id',1)
-
->update(['name'=>'thinkphp']);
-
// 資料中包含主鍵,直接使用
-
Db::table('tp_user')
-
->update(['name'=>'thinkphp','id'=>1]);
-
// update方法返回影響資料的條數,沒修改任何資料返回0
-
// 使用Sql函式
-
Db::table('tp_user')
-
->where('id',1)
-
->update([
-
'login_time'=>['exp','now()'],
-
'login_times'=>['exp','login_times+1']
-
]
-
);
-
// 更新某個欄位的值
-
Db::table('tp_user')
-
->where('id',1)
-
->setField('name','thinkphp');
-
// 自增自減
-
// setInc/setDec
-
Db::table('tp_user')
-
->where('id',1)
-
->setInc('score');
-
Db::table('tp_user')
-
->where('id',1)
-
->setInc('score',5);
-
Db::table('tp_user')
-
->where('id',1)
-
->setDec('score');
-
Db::table('tp_user')
-
->where('id',1)
-
->setDec('score',5);
-
// 延遲更新
-
Db::table('tp_user')->where('id',1)->setInc('score',1,10);
-
// 助手函式
-
db('user')->where('id',1)->update(['name'=>'thinkphp']);
-
db('user')->where('id',1)->setField('name','thinkphp');
-
db('user')->where('id',1)->setInc('score');
-
db('user')->where('id',1)->setDec('score');
-
// 快捷更新
-
Db::table('tp_user')
-
->where('id',1)
-
->inc('read')
-
->dec('score',3)
-
->exp('name','UPPER(name)')
-
->update();
-
}
-
}