Jump to content

MySQL - Find values in a field of serialized data


Deji

Recommended Posts

Bit hard to explain it in a topic title... I have my own idea on how to do this, but it's a long shot and I sense some flaws in it.I have a database with information on car models. I'm serializing arrays and storing it in a single table called "specs" - which is needed since I have little clue as to how many 'specs' there are going to be.. Regardless, I think lots of rows is a bad idea.I'd seen other MySQL Tables use similar methods to store data. However, now I want a search form that can find certain values for the cars...For example, if the user wanted a car of a certain type... My best guess towards figuring this out is:

(AND specs LIKE)%"[Type]";i:1;s:%:"%{$_POST['type']}%";}%

(the array name of the car spec "type" is "[Type]" - my system is multilingual and this is replaced with text)Simply replaces the size specification of the array so that any is valid and puts the users search.This works fine, but I'm looking for validation of this technique as I sense it's a bit rustic. I probably only know basics on MySQL (never took a tutorial on it, since most of it was self-explanatory and easy to understand from other examples).As a side-question: How does soundex work when querying MySQL? Would the better version "metaphone" work with the queries? And is it something that can be done in my serialized array situation?

Link to comment
Share on other sites

More rows is better (much, much better) than having some quirky data structure that is separate from the database. In this case you should have another table, say called specs, that holds all possible specifications. Then you have a relational table, models_specs, that takes the ID for the model, the ID for the specification, and the value.Then to search you can just do, if the "type" specification had ID 1 (your search form should pass the ID, not the name, of the specification, e.g. through a select box):

SELECT * FROM models WHERE models.id = specs.model AND specs.type = 1 AND specs.value = "the_requested_value" GROUP BY models.id

P.S. This design process, which includes trying to reduce compound data sets squished into single fields, is known as normalization.

Link to comment
Share on other sites

But my website isn't based entirely on cars.. It's a pretty small part of the whole idea.To be more specific, I could add that the cars aren't even real. I didn't think this info would be part of the answer so I decided not to bother on specifics of the usage, I was mainly using it as an example. Chances are I'll have to use a technique like this in future, anyway.What's more, adding a new database is frankly a messy task with my host so I want to keep the number of extra tables low, since there are already about 120 tables taken by the IPB Forum and my other systems.This idea of using serialization was from the example set by the other tables that were created for IPB. They use serialization for all data for which there is not a guranteed number of fields.For example, I'm allowing user input and maybe my members will decide to add information about the cars handling... They may give information such as "acelleration", "mass" or even more advanced information about the car. Stuff which I know nothing about. I want to ensure that all this data will be displayed properly. With an array, showing the data inside is easy and can be done by automatic processes.However, a new table requires changing the code based on that table... And changing the table based on what people want to put into it. "you should have another table, say called specs, that holds all possible specifications" is not possible when I don't know half the specifications.It's a bit hard to explain.. but basically this is all done on one page. The page is simple and even uses Javascript functions to order the elements on the table. Everything is done but the search, for which there are automatic options. For example, if someone provided information on how many wheels the car has, a new search option would pop up allowing the user to search for the desired number of wheels. Well, maybe I won't go that deep (since there are only 212 cars in the database anyway, and most visitors know about them all) but I do this mainly for education and exploration... Good for next years college course and such.You're probably right about the database usage in the long run and I should some day read more into using databases, but I was looking for a solution on the PHP side of things, since my tables are already set up how I'm used to using them and I'm not looking for a database commited to this page in particular.Sorry for any confusion.

Link to comment
Share on other sites

I'll just mention this for reference and things, as it might be handy to know.

They may give information such as "acelleration", "mass" or even more advanced information about the car. Stuff which I know nothing about. I want to ensure that all this data will be displayed properly.
Its still possible to structure this nicely in the database without having to modify tables or do serialisation. You can do something like, you have a cars table, to store information that you always know will be present, and then have a related car_properties table, which, as well as storing a reference to the cars table, holds a label and value.This lets you store any arbitrary fields you like, but without any need for serialisation.Just in case this isn't clear, the data might look roughly like this:
car_id   | label			| value--------------------------------------------------1		| mass			 | 7001		| acceleration	 | 6.72		| fuel			 | diesel2		| mass			 | 12003		| acceleration	 | 0 - 60 in 8.3 seconds

Link to comment
Share on other sites

I decided not to do it because of more tables and more queries that have to be done. I've used that technique plenty for my downloads system, though.Still, it seems my way of searching within a serialized array is probably the only way.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...