Comparison of Mixed fields in Arrays for complex thinkphp queries

Source: Internet
Author: User
I don't know how to express the title. The approximate meaning is: how to add a comparison field query after the SQL statement query in thinkphp uses an array as the query condition. In the thinkphp manual, it is recommended to use Arrays for queries, which is highly secure. However, I encountered a problem. When we use Arrays for comparison

I don't know how to express the title. The approximate meaning is: how to add a comparison field query after the SQL statement query in thinkphp uses an array as the query condition. In the thinkphp manual, it is recommended to use Arrays for queries, which is highly secure. However, I encountered a problem. When we use Arrays for comparison

I don't know how to express the title. The approximate meaning is: how to add a comparison field query after the SQL statement query in thinkphp uses an array as the query condition.

In the thinkphp manual, it is recommended to use Arrays for queries, which is highly secure. However, I encountered a problem. When we compare fields in the array form, we cannot compare fields, which is depressing. The Code is as follows:

First, the SQL statement I need is like this.

select * from post where (id = '100') and (user_id = `user`.user_id)

This is the way TP uses arrays.

$sql = M('post')$map['id']??=?array('eq',100);$map['user_id']??=?array('eq','user.user_id');$sql->where($map)->$sql;

The SQL statement to survive is as follows:

select * from post where (id = '100') and (user_id = 'user.user_id')

The field comparison in the latter part is changed to a comparison string because the single quotation mark is added. Not the statement I want.

After reading thinkphp's official manual, no solution was found, and Google Baidu did not. Later, I had an opportunity to figure out whether the where statement can be repeatedly written twice? Change the code to the following:

$sql = M('post')$map['id']??=?array('eq',100);$sql->where($map)->where('user_id = user.user_id')->$sql;

Run $ SQL-> getlastsql (); print to obtain the following SQL:

select * from post where (id = '100') and (user_id = `user`.user_id)

Oh! This is the SQL statement I need. It seems that the where clause can be called repeatedly.

Finally, I spoke about the thinkphp manual, which was not mentioned in many of the above knowledge points. It looks really hard. Fortunately, this is worth the effort. Now thinkphp is quite comfortable.

Original article address: Comparison of fields in the array mixed with thinkphp complex query. Thank you for sharing it with the original author.

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.