Description of fleaphp [database] query condition ($ conditions)

Source: Internet
Author: User

In fleaphp, all functions used for database query require the query condition parameter $ conditions. The usage is as follows:

Example:

View plaincopy to clipboardprint?
// $ Conditions Save the <B style = "color: black; Background-color: RGB (153,255,153);"> query </B> 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 );
// $ Conditions save query Conditions
$ 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); $ conditions parameters can be integers, strings, and arrays:

1. If the $ conditions parameter is an integer, it is assumed that the integer is the value of the primary key field.

View plaincopy to clipboardprint?
// <B style = "color: black; Background-color: RGB (153,255,153);"> query records whose primary key field value is 1 </B>
$ User = $ tableusers-> Find (1 );
// If the primary key field name is "ID", the generated where statement is "Where 'id' = 1"
// 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:

View plaincopy to clipboardprint?
$ Conditions = 'id <3 ′
$ User = $ tableusers-> Find ($ conditions );
// The generated where statement is "Where id <3"
$ 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:

View plaincopy to clipboardprint?
// <B style = "color: black; Background-color: RGB (153,255,153);"> query records whose ID field value is 3
$ Conditions = array (
'Id' => '1 ′,
);
$ User = $ tableusers-> Find ($ conditions );
// The generated where statement is "Where 'id' = 1"
// Query records whose ID field value is 3
$ Conditions = array (
'Id' => '1 ',
);
$ User = $ tableusers-> Find ($ conditions );
// The generated where statement is "Where 'id' = 1" 3. 2. 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:

View plaincopy to clipboardprint?
$ Conditions = array ('Id = 1 ′);
// The generated where statement is "Where 'id' = 1"
$ User = $ tableusers-> Find ($ conditions );
$ Conditions = array ('Id = 1 ');
// The generated where statement is "Where 'id' = 1"
$ User = $ tableusers-> Find ($ conditions); 3.3. $ when conditions is an array, you can mix string and key-value pairs:

View plaincopy to clipboardprint?
$ Conditions = array (
'Id <3 ′,
'Sex '=> 'male ',
);
$ User = $ tableusers-> Find ($ conditions );
// The generated where statement is "id <3 and 'sex' = 'male '"
$ Conditions = array (
'Id <3 ',
'Sex '=> 'male ',
);
$ User = $ tableusers-> Find ($ conditions );
// When the generated where clause is "id <3 and 'sex '= 'male'" $ conditions is an array, multiple query conditions are connected using the and boolean operator.

. 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?

View plaincopy to clipboardprint?
// If the primary key is named "ID", <B style = "color: black; Background-color: RGB (153,255,153) is required ); "> query </B> If the id value is one of 1, 2, and 3, you can write it as follows:
$ Condition = array (
'In () '=> array (1, 2, 3 ),
)
$ User = $ tableusers-> Find ($ conditions );
// The generated where clause is "Where 'id' in (1, 2, 3 )"
// 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 it like this:
$ Condition = array (
'In () '=> array (1, 2, 3 ),
)
$ User = $ tableusers-> Find ($ conditions );
// If the generated where clause is "Where 'id' in (1, 2, 3)", what should I do if it is not a primary key? It is also very easy to provide key-value pairs. For example:

View plaincopy to clipboardprint?
$ Condition = array (
'In () '=> array (
'Username' => array ('username1', 'username2 ′)
)
)
$ User = $ tableusers-> Find ($ conditions );
// The generated where clause is "Where 'username' in ('username1', 'username2 ′)"
$ Condition = array (
'In () '=> array (
'Username' => array ('username1', 'username2 ')
)
)
$ User = $ tableusers-> Find ($ conditions );
// The generated where clause is "Where 'username' in ('username1', 'username2')" 4. 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:

View plaincopy to clipboardprint?
$ Fields = array ('title', 'created ');
// You can also write the following string. The two methods have the same effect. The difference is that the automatically generated field names are added with the "'" symbol on both sides, to prevent conflicts between field names and SQL keywords. We recommend that you add the character "'" to your handwriting.
$ Fields = 'title, created ';
$ User = $ tableusers-> Find ('Id <10', null, $ fields );
$ Fields = array ('title', 'created ');
// You can also write the following string. The two methods have the same effect. The difference is that the automatically generated field names are added with the "'" symbol on both sides, to prevent conflicts between field names and SQL keywords. We recommend that you add the character "'" to your handwriting.
$ Fields = 'title, created ';
$ User = $ tableusers-> Find ('Id <10', null, $ fields); we recommend that you use arrays to process table data entries faster.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/phpsome/archive/2009/03/19/4006069.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.