This paper describes the fleaphp Framework database query conditions ($conditions). Share to everyone for your reference, as follows:
In fleaphp, all the functions that use database query need to query the condition parameter $conditions, now the usage is as follows:
Example:
$conditions Save Query conditions $conditions = ' Level_ix > 1 ';//$tableOrders is a table data entry object for an order data table er = $tableOrders->find ($ Conditions, ' created DESC ', ' ID, title, Body '), $conditions = Array (' username ' = ' dualface ');//$tableUsers is a user information data sheet The table data entry Object $user = $tableUsers->find ($conditions);
$conditions parameters can be integers, strings, and arrays of three types:
1. If the $conditions parameter is an integer, the integer is assumed to be the primary key field value.
Query for records with a primary key field value of 1 $user = $tableUsers->find (1);//If the primary key field is named "id", the resulting where clause is "where ' id ' = 1"
2. If the $conditions parameter is a string, the string will be used directly as a query condition, which can support the most flexible query conditions. For example:
$conditions = ' ID < 3 ' $user = $tableUsers->find ($conditions);//The WHERE clause generated 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 equals the key value. For example:
The Query ID field value is 3 records $conditions = Array ( ' id ' = ' 1 ',), $user = $tableUsers->find ($conditions);//The generated WHERE clause is " WHERE ' id ' = 1 "
3.2. If the $conditions parameter is an array, but the element does not have a key name, the key value is assumed to be a custom query condition, for example:
$conditions = Array (' id = 1 ');//The WHERE clause generated is "where ' id ' = 1" $user = $tableUsers->find ($conditions);
3.3. When $conditions an array, you can mix strings and key-value pairs in two flavors:
$conditions = Array ( ' ID < 3 ', ' sex ' = ' male '), $user = $tableUsers->find ($conditions);//The generated WHERE clause is "ID < 3 and ' sex ' = ' male '"
When $conditions an array, the and Boolean operators are used to connect between multiple query conditions.
3.4. The implementation of the "in ()" Query in fleaphp. (Originally published by Dreampig in http://www.fleaphp.org/bbs/viewthread.php?tid=2168)
We sometimes have to use in such operations, then how to write in the condition?
If the primary key is named "id", one of the values of the query ID is 1, 2, 3, you can write this: $condition = Array ( ' in () ' = = Array (three-way),) $user = $tableUsers Find ($conditions);//The generated WHERE clause is "where ' id ' in (1, 2, 3)"
So what if it's not the primary key? It is also simple 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 ')"
The meanings and usages of other parameters in the 4.find () function are as follows:
4.1. $sort parameter specifies how to sort the query when the type is only a string
For example ' created ASC ' means sorting from small to large according to the ' Created ' field.
4.2. $fields parameter specifies which fields are to be included in the query results, and the type can be a string or an array
When you have a large number of fields in a datasheet, you can improve performance by specifying $fields parameters to avoid querying unwanted fields.
The $fields parameter can be either a comma-delimited field name or an array with multiple field names, for example:
$fields = Array (' title ', ' created ');//can also be written in the following string form, the same as the two, the difference is that the automatically generated field names will be added on both sides of the "'" symbol, to prevent the occurrence of a field name conflict with the SQL keyword occurs. The suggested handwriting is also added "'" character $fields = ' title, created '; $user = $tableUsers->find (' ID < ', NULL, $fields);
It is recommended to use an array so that the table data entry is processed faster.
It is hoped that this article is helpful to the PHP program design based on fleaphp framework.