Thinkphp interval query, statistical query, and SQL direct query of instance Analysis _ php instance

Source: Internet
Author: User
This article mainly introduces thinkphp range query, statistical query, and SQL direct query. it analyzes the common query techniques of ThinkPHP in details in the form of examples, which is very useful, for more information about the thinkphp interval query, statistical query, and SQL direct query, see the examples in this article. Share it with you for your reference. The specific method is as follows:

I. interval query:

The code is as follows:

$ Data ['id'] = array ('GT ', 4), array ('Lt', 10); // The default relationship is (and) and
// SELECT * FROM 'TP _ user' WHERE ('id'> 4) AND ('id' <10 ))

$ Data ['id'] = array ('GT ', 4), array ('Lt', 10), 'OR') // The link is (or) or relationship

$ Data ['name'] = array ('like', '% 100'), array ('like',' % '), 'gege ', 'or ');


Multiple arrays can be added to the array. if there is no or, the relationship is "and" by default.

II. statistical query:

Count // Obtain the number
Max // obtain the maximum number
Min // obtain the minimum number
Avg // Obtain the average
Sum // get the sum

The code is as follows:

$ M = M ('user ');
$ Arr = $ m-> count (); // obtain the total number of users.
$ Arr = $ m-> where ("username = 'gege'")-> count (); // place the string

$ M = M ('user ');
$ Data ['username'] = 'gege'; // place an array
$ C = $ m-> where ($ data)-> count (); // The array operation is more standard.

III. SQL direct query.

A. The primary number of queries processes the data read. The result set is returned successfully. if the query fails, the return value is boolean false.

The code is as follows:

$ M = M ();
$ Result = $ m-> query ("select * from tp_user where id> 50 ");
Var_dump ($ result );


B. execute is used to update write operations. if a write operation is successful, the number of affected rows is returned. if a write operation fails, the return value is boolean false.

The code is as follows:

$ M = M ();
$ Result = $ m-> execute ("insert into tp_user ('Username') values ('ztz3 ')");
Var_dump ($ result );

I hope this article will help you with ThinkPHP framework programming.

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.