1, database configuration
return [ 'type' => 'MySQL', // database type 'hostname' => '127.0.0.1', // server address ' Database '=>' Test ', // database name ' username '=>' root ', // database username ' password '=>' ', // database password ' HostPort '=>' ', // database connection port ' the params' => [], // database connection parameters ' charset '=> 'UTF8', // database using default encoding UTF8 'prefix' => '',// database table prefix 'Debug' => to true , // database debug mode ];
// long connection
return[ 'Database' => 'Demo',//database name 'prefix' => 'think_',//database table prefix 'the params' => [//database connection parameters // use long link \ :: ATTR_PERSISTENT the PDO =>to true, ], ];
database configuration file // module and only need to configure part of the global database configuration file differences, the same configuration need not be repeated.
2, the native query: a query and Execute, respectively, and for writing queries
$result = Db::execute('insert into think_data (id, name ,status) values (5, "thinkphp",1)');// 增 $result = Db::execute('delete from think_data where id = 5 ');// 删 $result = Db::execute('update think_data set name = "framework" where id = 5 ');// 改 $result = Db::query('select * from think_data where id = 5');// 查
// switch database query 'DB1' => [ //database configuration. 1 ... ], 'DB2' => [ //Database Configuration 2 ... ], //link Db :: connent ( 'db1')> Query ( 'SELECT * from think_users'); Db:: connent ( 'DB2') -> Query ( 'SELECT * from think_data');
3, query builder
// Insert recording Db :: name ( 'Data' ) -> INSERT ([ 'ID' => 18 is, 'name' => 'thinkphp' ]); // update record Db :: name ( 'Data' ) - > WHERE ( 'ID', 18 is ) -> Update ([ 'name' => "Framework" ]); // query data Db :: = List name $ ( 'data') -> WHERE ( 'ID', 18 is ) -> SELECT ();
// delete the data Db :: name ( 'data' ) -> WHERE ( 'ID', 18 is ) -> delete ();
// insert multiple records
Db :: name ( 'data' )
-> insertAll ([[ 'ID' =>. 19, 'name' =>'John Doe'], [ 'id' = > 20, 'name' => ' John Doe'], ...]);
//db
helper default reconnected each time the database, multiple calls should therefore be avoided .
4, the operation chain: the chain, regardless of before and after the operation, but must be () or the like in front of the select operation
// query and ten data conditions are met in accordance with the reverse order id $ List = Db :: name ( 'Data' ) -> WHERE ( 'Status',. 1 ) -> Field ( 'id, name' ) -> Order ( 'ID', 'desc' ) -> limit (10 ) -> SELECT ();
5, transaction support
...