Read data
$ M = new Model ('user ');
$ M = M ('user ');
Select
$ M-> select (); // obtain all data and return it as an array
Find
$ M-> find ($ id); // obtain a single data record
GetField (field name) // obtain a specific field value
$ Arr = $ m-> where ('Id = 2')-> getField ('username ');
Iii. ThinkPHP 3 create data (important)
Create
$ M = new Model ('user ');
$ M = M ('user ');
$ M-> Field name = Value
$ M-> add ();
The returned value is the new ID number.
Iv. ThinkPHP 3 Delete data (important)
$ M = M ('user ');
$ M-> delete (2); // delete data with id 2
$ M-> where ('Id = 2')-> delete (); // the data with the id of 2 is also deleted.
The returned value is the number of affected rows.
5. ThinkPHP 3 update data (important)
$ M = M ('user ');
$ Data ['id'] = 1;
$ Data ['username'] = 'ztz2 ';
$ M-> save ($ data );
The returned value is the number of affected rows.
========================================================== ====
I. Common Query Method
Ii. Expression Query Method
Iii. interval Query
Iv. Statistical Query
V. Direct SQL query
I. Common Query Method
A. String
$ Arr = $ m-> where ("sex = 0 and username = 'give'")-> find ();
B. Array
$ Data ['sex'] = 0;
$ Data ['username'] = 'gege ';
$ Arr = $ m-> where ($ data)-> find ();
Note: This method defaults to the relationship between and. If you use the or relationship, you need to add an array value.
$ Data ['sex'] = 0;
$ Data ['username'] = 'gege ';
$ Data ['_ logic'] = 'or ';
Ii. Expression Query Method
$ Data ['id'] = array ('lt ', 6 );
$ Arr = $ m-> where ($ data)-> select ();
Equal to EQ
NEQ is not equal
GT>
EGT greater than or equal
LT is less
ELT less than or equal
LIKE fuzzy query
$ Data ['username'] = array ('like', '% Ge ');
$ Arr = $ m-> where ($ data)-> select ();
NOTLIKE
$ Data ['username'] = array ('notlike', '% ge %'); // there is no space in the notlike Center
$ Arr = $ m-> where ($ data)-> select ();
Note: If a field matches multiple wildcards
$ Data ['username'] = array ('like', array ('% ge %', '% 100',' % 5% '),' and '); // if there is no third value, the default relationship is or.
$ Arr = $ m-> where ($ data)-> select ();
BETWEEN
$ Data ['id'] = array ('between', array (5, 7 ));
$ Arr = $ m-> where ($ data)-> select ();
// SELECT * FROM 'tp _ user' WHERE ('id' BETWEEN 5 AND 7 ))
$ Data ['id'] = array ('not between', array (5, 7); // note that spaces must exist between not and.
$ Arr = $ m-> where ($ data)-> select ();
IN
$ Data ['id'] = array ('in', array (4,6, 7 ));
$ Arr = $ m-> where ($ data)-> select ();
// SELECT * FROM 'tp _ user' WHERE ('id' IN (4,6, 7 ))
$ Data ['id'] = array ('not in', array (, 7 ));
$ Arr = $ m-> where ($ data)-> select ();
// SELECT * FROM 'tp _ user' WHERE ('id' not in (4,6, 7 ))
Iii. interval Query
$ Data ['id'] = array ('gt ', 4), array ('lt', 10); // The default relationship is the relationship between and.
// SELECT * FROM 'tp _ user' WHERE ('id'> 4) AND ('id' <10 ))
$ Data ['id'] = array ('gt ', 4), array ('lt', 10), or ') // The relationship is the or relationship.
$ Data ['name'] = array ('like', '% 100'), array ('like',' % 5% '), 'gege ', 'or ');
Iv. Statistical Query
Count // obtain the number
Max // obtain the maximum number
Min // obtain the minimum number
Avg // obtain the average
Sum // get the sum
V. Direct SQL query
A. query mainly processes the read data
Result set of successfully returned data
Boolean false is returned for failure.
$ M = M ();
$ Result = $ m-> query ("select * from t_user where id> 50 ");
Var_dump ($ result );
B. execute is used to update write operations.
Number of affected rows returned successfully
Boolean false is returned for failure.
$ M = M ();
$ Result = $ m-> execute ("insert into t_user ('username') values ('ztz3 ')");
Var_dump ($ result );