Retrieving Data from Serialized Arrays in MySQL
Problem:
You want to query a database table where a field contains a serialized array and select only records that contain a specific item from the array. Specifically, you need to find records where "an item in my array" equals '$n'.
Solution:
Using a serialized array for such a task is not an optimal solution, but if you are unable to store the data differently, you can utilize PHP's serialization mechanism to perform the search.
Implementation:
Recommendation:
As mentioned by other contributors, it is highly advisable to consider storing the data in a normalized fashion if you anticipate frequent queries based on array contents. This will significantly improve performance and maintainability.
The above is the detailed content of How can I efficiently query a MySQL table with a serialized array field to find records containing a specific item?. For more information, please follow other related articles on the PHP Chinese website!