FleaPHP framework database query condition ($ conditions) statement summary, fleaphpconditions. FleaPHP framework database query condition ($ conditions) writing summary, fleaphpconditions this article describes the FleaPHP framework database query condition ($ conditions) writing. I would like to share with you the FleaPHP framework database query conditions ($ conditions) written summary, fleaphpconditions
This article describes how to write a query condition ($ conditions) statement for the FleaPHP framework database. We will share this with you for your reference. The details are as follows:
In FleaPHP, all functions used for database query require the query condition parameter $ conditions. The usage is as follows:
Example:
// $ Conditions save the query condition $ conditions = 'level _ ix> 1 '; // $ tableOrders is the table data entry object of an order data table $ order = $ tableOrders-> find ($ conditions, 'created desc', 'id, title, body '); $ conditions = array ('username' => 'dualface '); // $ tableUsers is a table data entry object of the user information data table $ user = $ tableUsers-> find ($ conditions );
The $ conditions parameter can be an integer, a string, or an array:
1. if the $ conditions parameter is an integer, it is assumed that the integer is the value of the primary key field.
// Query records whose primary key field value is 1 $ user = $ tableUsers-> find (1); // if the primary key field name is "id ", the generated where statement is "WHERE 'id' = 1"
2. if the $ conditions parameter is a string, the string is directly used as a query condition. this method supports the most flexible query conditions. For example:
$ Conditions = 'Id <3' $ user = $ tableUsers-> find ($ conditions); // The generated where statement is "WHERE id <3"
3. 1. if the $ conditions parameter is an array and the key name and value are specified, the field name in the query condition is the key name, and the field value is equal to the key value. For example:
// Query records whose id field value is 3 $ conditions = array ('id' => '1',); $ user = $ tableUsers-> find ($ conditions ); // The generated where statement is "WHERE 'id' = 1"
. If the $ conditions parameter is an array but the element does not have a key name, it is assumed that the key value is a custom query condition. for example:
$ Conditions = array ('Id = 1'); // The generated where statement is "WHERE 'id' = 1" $ user = $ tableUsers-> find ($ conditions );
3.3. $ conditions is an array, you can mix string and key-value pairs:
$ Conditions = array ('Id <3 ', 'sex' => 'male',); $ user = $ tableUsers-> find ($ conditions ); // The generated where statement is "id <3 AND 'sex' = 'male '"
When $ conditions is an array, multiple query conditions are connected using the AND Boolean operator.
3.4. implementation of "in ()" query in FleaPHP. (Originally published by DreamPig in http://www.fleaphp.org/bbs/viewthread.php? Tid = 2168)
We sometimes need to use operations like in, so how can we write in condition?
// If the primary key is named "id" and you need to query one of the values of id 1, 2, and 3, you can write: $ condition = array ('In () '=> array (1, 2, 3),) $ user = $ tableUsers-> find ($ conditions); // The generated where clause is "WHERE 'id' IN (1, 2, 3 )"
So how can I write it if it is not a primary key? It is also very easy to provide key-value pairs. For example:
$ Condition = array ('In () '=> array ('username' => array ('username1', 'username2 '))) $ user = $ tableUsers-> find ($ conditions); // The generated where clause is "WHERE 'username' IN ('username1', 'username2 ')"
4. the meanings and usage of other parameters in the find () function are as follows:
4.1. The $ sort parameter specifies the sorting method for the query. the type can only be string.
For example, 'created ASC 'indicates to sort by "created" field from small to large.
4.2. $ fields parameter specifies the fields to be included in the query result. the type can be string or array.
When there are many fields in the data table, you can specify the $ fields parameter to avoid unnecessary fields, thus improving performance.
The $ fields parameter can be a comma-separated field name, or an array containing multiple field names. for example:
$ Fields = array ('title', 'created '); // You can also write the following string format. The two methods share the same purpose, the difference is that the automatically generated field names are added with the "'" symbol to prevent conflicts between the field names and SQL keywords. We recommend that you add the "'" character $ fields = 'title, created' to your handwriting; $ user = $ tableUsers-> find ('Id <10', NULL, $ fields );
We recommend that you use arrays to process table data more quickly.
I hope this article will help you design PHP programs based on the FleaPHP framework.
Articles you may be interested in:
- Php implements cvs data import to MySQL based on Fleaphp framework
- Fleaphp rolesNameField bug solution
- How to use the find function in fleaphp crud operations
- How to use the findByField function for fleaphp crud operations
- Common fleaphp method Pager usage
- FleaPHP security settings
- Clever solutions to uncertain multi-condition queries under fleaphp
Fleaphpconditions describes the query condition ($ conditions) of the FleaPHP framework database. I will share it with you for your reference...