Connecting to a Database
-
Connecting to the default database
Db::connect()
-
Connect to the second database defined
Db::connect(‘db2’);
Database query
The way SQL statements are used
Db::query("select * from gary_user where id=?" [1]),Copy the code
Select returns all records. The result is a two-dimensional array
If the result does not exist, return an empty array
Db::table('gary_user') ->select()
Copy the code
Find returns a record, which returns a one-dimensional array
Returns NULL if the result does not exist
Db::table('gary_user') -> find();
Copy the code
Value returns a record and is the value of a field in the note record
Returns NULL if the result does not exist
Db::table('gary_user') -> value('password');
Copy the code
Column returns a one-dimensional array whose value is the value we want to fetch
If a second argument exists, the array is returned and the value of the second argument is used as the key value
If the result does not exist, return an empty array
Db::table('gary_user') -> column('password');
Copy the code
Name does not need a prefix
Db::name('user') ->select()
Copy the code
Db helper function
db('user',[],false) -> find()
Copy the code
Add the data
The return value of insert affects the number of rows in the record
$db = Db::name('user'); $db -> insert([ 'email' => '[email protected]', 'password' =>md5('gary_01'), 'username' =>'gary_01' ]);
Copy the code
InsertGetId returns the incremented ID of the inserted data
$res = $db -> insertGetId([ 'email' => '[email protected]', 'password' =>md5('gary_02'), 'username' =>'gary_02' ]);
Copy the code
InsertAll returns the number of rows that successfully inserted data
$data = []; for($i =0; $i<10; $i++){ $data[] = [ 'email' => "1@QQ{$i}.COM", 'username' => 'gary', 'password' => "gary_{$i}" ]; }; $db -> insertAll($data);Copy the code
Database update
Update returns the number of affected rows
$db -> where([ 'id' => '1' ]) -> update([ 'username' => '1205' ]);
Copy the code
SetField returns the number of rows affecting data. Only one argument can be changed
$db -> where([ 'id' => '1' ]) -> setField([ 'username' => '1205' ]);
Copy the code
SetInc returns an increment in the number of rows affecting data
$db -> where([ 'id' => '1' ]) -> setInc('num',3);
Copy the code
SetInc returns decrement of the number of rows affecting data
$db -> where([ 'id' => '1' ]) -> setDec('num',3);
Copy the code
Deletions must conditionally return the number of deleted rows
$res = $db ->where([ 'password' => 'gary_2' ]) -> delete();
Copy the code
Conditional constructor
$res = $db ->where("id", 'LT', 1) ->where("id", 'LT', 1) ->whereOr("username", 'GT', 1) ->buildSql();
Copy the code
-
EQ =
-
NEQ <>
-
LT <
-
ELT <=
-
GT >
-
EGT >=
-
BETWEEN BETWEEN * AND *
-
NOTBETWEEN NOTBETWEEN * AND *
-
IN IN (*,*)
-
NOTIN NOT IN (*,*)