How to handle the problem of using arrays in php as mysql Query conditions wherexxinarray

Source: Internet
Author: User
Ask the php array as the mysql Query condition wherexxinarray, how to deal with the original thinking to split the array $ mermerid, determine the length to different variables, and determine the number of variables, then write the mysql query below. it may be a bit difficult. could you please ask if you have any good methods? thank you!

---------------------------------------------
$ Customerid:

Array (size = 2)
0 => string '5' (length = 1)
1 => string '14' (length = 2)
----------------------------------------------

$ Customer-> where ("id in". $ customerid)-> count (); // This means that, of course, it is incorrect to put the array directly here.


Reply to discussion (solution)

Because count is counted, the mysql statement below should be one. It is not good to loop through one query.

Foreach ($ customerid as & $ value ){
$ Count = $ customer-> where ("id in". "($ value)")-> count ();
$ Arrcount [] = $ count;
}

$ Count = 0;
Foreach ($ arrcount as & $ value ){
$ Count = $ count + $ value;
}

Solved, and new problems emerged.

How to control, different conditions, (display the first 10 Records)
For example
Where ("id in". (3) has 2
Where ("id in". (2) has 1

Actually, what products are added to each user's favorites. A user may add multiple favorites.
I want to display the first 10 items they have summarized. I do not know how many times they have been combined for query, and the number of times they have been recycled is very troublesome.
Is it best to change the table structure?

In the product table, add a userid? Can a single user add different products to their favorites? It seems that this design works.

The first problem is that if you want to query a single id, you can directly use id = xx, there is no need to use in
If you want to query multiple IDs, combine the array with the implode function into a string in the format of id in (1, 2, 3 ).
The final problem is that if you want to output the user list and output the first 10 favorites of each user, you need to consider caching




Every cool

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.