This article will mention:
1. native query operation reads: query ()
2. native query operation writes: insert / update / delete, execute ()
3. Query Builder
4.where () function: can query based on other fields
5.order (), limit () function
For the configuration database in the frame app \ config \ database.php has been configured, it will default to the following has been connected to the database
As a data table example shows:
1. native query operation reads: query ()
public function demo1() { $sql = "SELECT `userName` FROM `admin` WHERE `id`=:id "; $map = ['id' => 1]; $res = Db::query($sql, $map); dump($res);//打印查询结果$res }
2. native query operation writes: insert / update / delete, execute ()
public function demo2 () { $ SQL = "status` the UPDATE =` `admin` the SET: the WHERE Status =` id`: ID " ; $ Map = [ 'ID' =>. 1, 'Status' => 0 ]; $ RES = Db :: Execute ( $ SQL , $ Map ); return 'successfully updated'. $ RES 'record'. ; }
Data table id = status 1 from 1 to 0
3. Query Builder
① find (): returns the first record satisfying the condition, a single record; no return null
// Table (): Set the data table // field (): set the query field list public function Demo3 () { $ RES = Db :: Table ( 'ADMIN' ) -> Field ( 'ID, the userName, Phone' ) - > Find (2); // supports the primary key as a parameter: the WHERE `2 = id` the dump ( $ RES ); }
②select (): returns multiple records satisfy the conditions
public function demo4 () { $ RES = Db :: Table ( 'ADMIN' ) -> Field ( 'ID, the userName, Phone' ) // NOTE: select (2, 3) returns only the first; select ([2 3]) should be placed in the array -> SELECT (); // above the dump ( $ RES ); }
. 4 where () function: can query based on other fields
Set the search criteria main types: string, expressions, arrays
(1) String
public function demo5 () { $ RES = Db :: Table ( 'ADMIN' ) -> Field ( 'ID, the userName, Phone') // Field () returns only the corresponding fields // character string. -> WHERE ( ' Status> 0 ' ) -> SELECT (); the dump ( $ RES ); }
(2) Expressions
public function demo5 () { $ RES = Db :: Table ( 'ADMIN' ) -> Field ( 'ID, the userName, Phone') // Field () returns only the corresponding fields @ 2 expression: Recommended. -> WHERE ( 'ID', 'BETWEEN', [l, 3]) // parameter format: field, operator, value -> SELECT (); the dump ( $ RES ); }
(3) an array
① associative array: the equivalent of inquiry, AND
public function demo5 () { $ RES = Db :: Table ( 'ADMIN' ) -> Field ( 'ID, the userName, Phone') // Field () returns only the corresponding fields // ① associative array -> where ([ ' ID '=>. 1,' Phone '=> 123 ]) -> SELECT (); the dump ( $ RES ); }
Sql statement print look (in where () and select () is inserted into the intermediate code below)
->fetchSql(true)
② indexed array: batch query
public function demo5() { $res = Db::table('admin') ->field('id, userName, phone')//field()仅返回对应字段 //②索引数组 ->where([ // ‘0=>’ 索引值可以忽略,默认值 0 => ['id','between',[1,3]], 1 => ['status','>',0] ]) ->select(); dump($res); }
5.order(),limit()函数
①单字段排序
//order(),limit() public function demo6() { $res = Db::table('admin') ->field('id, userName, phone') //单字段排序 ->order('id', 'desc')//默认升序asc,desc:降序 ->limit(2,2)//从第2行开始的2条数据,常用于分页查询 ->select(); dump($res); }
②多字段排序,用数组参数
讲单字段排序中order()一行改为以下代码即可
->order(['id'=>'asc', 'phone'=>'desc'])