tp5-5 연결, 조작, 디버깅 데이터베이스
183229 단어 데이터베이스
모델을 조작할 때 지정한 데이터베이스 연결을 자동으로 연결합니다. 설정 파일에 설정된 기본 연결 정보가 아니라
1 구성 파일 config를 적용합니다.php에 그룹 설정 추가
'machine'=> [ // 'type' => 'mysql', // 'hostname' => '127.0.0.1', // 'database' => 'machine', // 'username' => 'root', // 'password' => 'root', // utf8 'charset' => 'utf8', // 'prefix' => '', ], 'rbac'=>'mysql://root:root@localhost:3306/rbac#utf8',
2 :
- $machine = Db::connect('machine')->query('select * from admin_users where id=1');
-
- $rbac = Db::connect('rbac')->query('select * from role where id=1');
3 :
php namespace app\admin\model; use think\Model; use think\Db; class Access extends Model { protected $connection = [ // 'type' => 'mysql', // 'hostname' => '127.0.0.1', // 'database' => 'rbac', // 'username' => 'root', // 'password' => 'root', // utf8 'charset' => 'utf8', // 'prefix' => '', ]; } 4
- public function useMode()
- {
- $a = new Access;
- dump($a::all());
- dump($a->query('select * from role where id=1'));
-
- $res = $a->table('access')->where('id','1')->find();
- dump($res);
-
- }
1
table
use think\Db; : // table Db::table('think_user')->where('id',1)->find(); find , null : Db::table('think_user')->where('status',1)->select(); select , name 'prefix' => 'think_', table
- Db::name('user')->where('id',1)->find();
- Db::name('user')->where('status',1)->select();
2 sql
//------------------------------- // | //------------------------------- dump(Db::getLastSql()); //------------------------------- // ->fetchSql() | //------------------------------- $data = Db::table('user')->fetchSql()->where($map)->select();
3 sql 1 execute
a
- public function insert()
- {
- $res = Db::execute("insert into user(username,password) values(' ','12345')");
- $res = Db::execute("insert into user(username,password) values(:name,:pa)",['name'=>' ','pa'=>'666']);
- dump($res);
- }
b
public function delete() { $res = Db::execute('delete from user where id >:id',['id'=>6]); dump($res); }
c
- public function update()
- {
- $res = Db::execute('update user set username=:name where id >=:id',['id'=>5,'name'=>' ']);
- dump($res);
- }
d
think\Db; // $result = Db::query('select * from think_user where id>? and id',[0,3]); $result = Db::query('select * from user where id>:id1 and id<=:id2',['id1'=>0,'id2'=>3]);
4 Db db ->find() ->select() 1 ( where( )) like whereOr limit 2 limit 2,3 order count sum find( ) field true column select find page group having join DISTINCT cache , cache 방법은 별도로 지정할 수 있습니다. fetchSql SQL
- php
- use think\Db;
- $map['id'] = ['>',1];
- $map['username'] = ['like','%u%'];
- $map['status'] = 0;
- //
- $data = Db::table('user')->where($map)->select();
-
-
- $data = Db::table('user')->where('id','10')->select();
-
- //
- $data = db('user')->where('id','10')->select();
- $data = db('user')->where('id','>=','4')->where('id',','9')->select();
- $data = db('user')->where('username','like','%u%')->where('id',','6')->select();
- $data = db('user')->where('id','>=','6')->whereOr('id','<=','2')->select();
- $data fetchSql= db('user')->where('id','>=','6')->whereOr('id','<=','2')->limit(2)->select();
- $data = db('user')->where('id','>=','6')->whereOr('id','<=','1')->limit(2,3)->select();
- $data = db('user')->where('id','>=','1')->whereOr('id','<=','1')->limit(2,6)->order('id','desc')->select();
- $data = db('user')->field('username nameis,count(*) as cou')->where('id','>=','1')->whereOr('id','<=','1')->limit(2,6)->order('id','desc')->select();
- $data = db('user')->field('count(*) as cou')->where('id','>=','1')->whereOr('id','<=','1')->limit(2,6)->order('id','desc')->find();
- $data = db('user')->field('username,password',true)->select();
-
- // column , , ,
- $data = db('user')->where('username','like','%u%')->column('username,password,status');
- /** page
- * page(1,5) :
- * page(2,5) :
- */
- $data = db('user')->page('2,5')->select();
- //group having
- $data = Db::table('user')->field('status,count(*) as co')->group('status')->select();
- $data = Db::table('user')->field('status,count(*) as co')->group('status')->having('co > 2')->select();
-
- //join
- $data = Db::table('user')->alias('u')
- ->where('u.id=:iidd',['iidd'=>1])
- ->field('u.username,ur.role_id,r.name')
- ->join('user_role ur','ur.uid=u.id')
- ->join('role r','r.id=ur.role_id')
- ->select();
-
- // DISTINCT
- Db::table('user')->distinct(true)->field('username')->select();
- //
- $data = Db::table('user')->where('id','10')->cache(true)->select();
- $data = Db::table('user')->where('id','10')->cache(60)->select(); // 60
-
- // cache :
- $data = Db::table('user')->cache('key',60)->find();
- $data = \think\Cache::get('key');
-
- //fetch
- $data = Db::table('user')->fetchSql(true)->find(1);
- result : SELECT * FROM think_user where id = 1
-
-
-
1 getLastInsID() insert($data);
2 insertGetId getLastInsID() insert
php $data = [ 'username'=>' ', 'password'=>'5555' ]; $resutl = Db::table('user')->insert($data); // $userId = Db::name('user')->getLastInsID(); // $resutl = Db::table('user')->insertGetId($data); // $data = [ [ 'username'=>' ','password'=>'5555'], [ 'username'=>' ','password'=>'5555'], ]; $resutl = Db::table('user')->insertAll($data); strong>
- php
- $data = [
- 'username'=>' ','password'=>'erere',
- ];
- $resutl = Db::table('user')->where('id', 2)->update($data);
p;\
php // $result = Db::table('user')->delete([12,13,14]); // $result = Db::table("user")->where('password','=','1111111')->delete();
1 count( ) 2 max( ) 3 min( ) 4 avg( ) 5 sum( )
- php
- $result = Db::table('user')->where('status','=','0')->count();
-
- $result = Db::table('user')->where('status','=','0')->avg('id');
php whereTime whereTime , : // Db::table('think_user')->whereTime('birthday', '>=', '1970-10-1')->select(); // Db::table('think_user')->whereTime('birthday', ', '2000-10-1')->select(); // Db::table('think_user')->whereTime('birthday', 'between', ['1970-10-1', '2000-10-1'])->select(); // Db::table('think_user')->whereTime('birthday', 'not between', ['1970-10-1', '2000-10-1'])->select(); , : // Db::table('think_blog') ->whereTime('create_time', 'today')->select(); // Db::table('think_blog')->whereTime('create_time', 'yesterday')->select(); // Db::table('think_blog')->whereTime('create_time', 'week')->select(); // Db::table('think_blog')->whereTime('create_time', 'last week')->select(); // Db::table('think_blog')->whereTime('create_time', 'month')->select(); // Db::table('think_blog')->whereTime('create_time', 'last month')->select(); // Db::table('think_blog')->whereTime('create_time', 'year')->select(); // Db::table('think_blog')->whereTime('create_time', 'last year')->select(); 、 、 , : // Db::table('think_blog')->whereTime('create_time', 'd')->select(); // Db::table('think_blog')->whereTime('create_time', 'w')->select(); // Db::table('think_blog')->whereTime('create_time', 'm')->select(); // Db::table('think_blog')->whereTime('create_time', 'y') ->select(); V5.0.5+ , // Db::table('think_blog')->whereTime('create_time','-2 hours')->select();
- php
- $id = input($id);
- $result = Db::table('banner_item')
- ->where(function($query) use ($id){
- $query->where('banner_id','=',$id);
- })
- ->select();strong>
5
mysql innodb
php use think\Db; // Db::startTrans(); try{ $a = Db::table('user')->delete(29); if(!$a)throw new \Exception(' '); $b = Db::table('user')->delete(90); if(!$b)throw new \Exception(' '); // Db::commit(); echo ' '; } catch (\Exception $e) { echo $e->getMessage(); // Db::rollback(); } // Db::startTrans(); $a = Db::table('user')->delete(26); $b = Db::table('user')->delete(90); if($a && $b) { Db::commit(); } else { Db::rollback(); } echo ' ';
application/database.php
//데이터베이스 디버그 모드 'debug' => true,
application/config.php 로그 열기
//로그 열기 'log' => [
//로그 기록 방식, 파일 소켓 내장 지원 확장 'type' = > 'file',//정상 File는 열려 있는 테스트이고 닫기 //로그 저장 디렉토리 'path' => LOG_PATH,
//로깅 레벨 'level' => ['sql'],
],
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SQLite의 query로 망설임이것은 내가 처음 안드로이드 응용 프로그램 개발에서 망설이고, 그 후 해결 된 방법을 비망록으로 철자하고 있습니다. java에서 SQLite를 이용한 애플리케이션을 작성하는 동안 EditText에 입력된 item이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.