Tom continues. How to use arrays as conditions for SQL query statements in PHP

Source: Internet
Author: User
Tom continues to seek help .. In PHP, how does one use arrays as conditions for SQL query statements. Thank you for your great help in your previous post and finally figured out what was going on. Now I encountered another problem: I asked the user to submit 12 multi-choice lists on one page, and obtained 12 Arrays. then I searched the database based on the contents of these 12 groups, however, it seems that there is no way to extract the content from these 12 groups one by one and then write SQL query statements .. For example, $ .. In PHP, how does one use arrays as conditions for SQL query statements.
Thank you for your great help in your previous post and finally figured out what was going on. Another problem is encountered:

On a page, I asked the user to submit 12 multi-choice lists,
12 arrays are obtained,

Then, the database is searched based on the content of the 12 groups,

However, it seems that there is no way to extract the content from these 12 groups one by one and then write SQL query statements ..

For example:

$ A = array (a, B, c, d );
$ B = array (1, 2, 4 );
$ C = array (I, II, III, IV );
Then I want to select * from table name where name = a AND name = B AND name = c AND name = d AND name = 1 AND name = 2 ..... AND name = IV

I don't know how to implement it? If you use a foreach loop, you are afraid that each time it is the value of the current element. in this way, you can find the result that can be searched by each current element...

However, I want to query the final result when all elements in all arrays exist ..

Help everyone!


------ Solution --------------------
Oh, so many AND may find something?
PHP code
$ A = array ('A', 'B', 'C', 'D'); $ B = array (1, 2, 3, 4 ); $ c = array ('I', 'II', 'II', 'IV '); # ensure that any non-empty array of $ a, $ B, $ c, echo "select * from table where name = '". implode ("'OR name ='", array_merge ($ a, $ B, $ c )). "'";
------ Solution --------------------
At dinner, please remind me
One is to output the array heat output string implode (',', $ arr) punctuation marks or something, and then process it by yourself.
One is to modify your SQL statement to change the same array AND or something to IN.
For example, IN ('A', 'B', 'C', 'D ')

You can.
------ Solution --------------------
Discussion

Reference:

At dinner, please remind me
One is to output the array heat output string implode (',', $ arr) punctuation marks or something, and then process it by yourself.
One is to modify your SQL statement to change the same array AND or something to IN.
For example, IN ('A', 'B', 'C', 'D ')

You can.


Thank you for your reply! If it is name IN ('A', 'B', 'C', 'D'), should it be an OR relationship? Or ......

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.