MySQL/PHP serialized array

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
So i'm busy ****ing around with this crappy "PHP" thing you guys are so fond of (lol)

I have a database column that contains a serialized array. Now it's piss reading the column/unserializing it and doing the array stuffs I need to do (holy ****, if I knew it was this easy working with arrays in PHP I'd have kicked ASP a long time ago)

ANYWAY

The issue now is that the client wants to query the data IN the serialized array that is stored in the MySQL column before it gets read out.

sort of like, select * from my**** where theserializedcolumncontainingarray.brandname = "Sony"

So the sample above is just pseudo code, the serialized array within the column in the db contains a column "brandname" and the dude wants to query it like above more or less because it obviously makes sense not to push through the entire dataset to php to process one by one right?

Help plox. Imma n00b
 

Drake2007

Expert Member
Joined
Oct 23, 2008
Messages
4,413
Is that datatype text?
$manufacturer = "Sony"
try this >
$strsql = "SELECT * FROM my**** WHERE theserializedcolumncontainingarray.brandname LIKE '%" . manufacturer . "%'"

Excuse the edits, I changed that example from my VBA code.
 
Last edited:

DarkStreet

Expert Member
Joined
Jan 18, 2007
Messages
1,284
Wont work Drake as you will match all similar manufactorer:

ie, searching for Sony and you have Sony,mysony,isony, will all return the same.

The SQL command you are looking for is ANY btw to resolve this!

http://dev.mysql.com/doc/refman/5.1/en/any-in-some-subqueries.html

ie: SELECT * FROM myProduct where SONY = ANY (brandname);

Don't think this will work as the serialized value may be something like "a:2:{s:2:"kw";s:0:"";s:6:"status";s:2:"-1";}".

Acid: if I may ask, what does the table contain (products?) and what data is being serialized?
 

Drake2007

Expert Member
Joined
Oct 23, 2008
Messages
4,413
obviously makes sense not to push through the entire dataset to php

I understood this to be only objective of the exercise?
 

Other Pineapple Smurf

Honorary Master
Joined
Jun 21, 2008
Messages
14,593
Sorry, I now understand the serialization is actually on the PHP side and not the DB side.

From experience I would avoid storing data in any database that cannot be parsed by the db client. I've never written stored procedures for MYSQL but that looks like your option here.
 

DarkStreet

Expert Member
Joined
Jan 18, 2007
Messages
1,284
obviously makes sense not to push through the entire dataset to php

I understood this to be only objective of the exercise?

Your suggestion would be the best bet here, but I'd like to hear from Acid what his DB setup is like because it sounds like it was not normalised properly.
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Basically the client has a system which you can assign your own custom tokens to data and then save it in the database. So obviously having the database alter physical columns based off of their input to create anything would be a bit silly. So what they did is when you upload the CSV to the database, you can "map" these tokens you created and it will then serialize those tokens + date into an array and store it in the database in 1 column.

So when you read from the database, you call out 1 record and unserialize the array and work with it as a normal thing (like "a:2:{s:2:"kw";s:0:"";s:6:"status";s:2:"-1";}")

If it were up to me I'd have done things differently, but I'm taking over the code so I can't really change any fundamental stuff. They already have a system where they search for the product name (however I can't find where it's at or if they use it for the search or not) and they want to add the ability to filter through manufacturer as well.

so if the custom token+data they uploaded contains a manufacturer column in the array, to have only those records matching select out of the database.

So basically:

Upload CSV -> Map token (column) to data -> serialize -> store in 1 column in database matched to the set of data uploaded

Now to read out I have to:

Select records -> unserialize everything -> loop through arrays

Now obviously if I can do that **** in MySQL before pushing it to PHP, I won't end up with 50 million records having to process in PHP before I can have a set of 100 products display matching my search criteria...

So that's my question

How can i search on a serialized column within mysql using t-sql
 

DarkStreet

Expert Member
Joined
Jan 18, 2007
Messages
1,284
Basically the client has a system which you can assign your own custom tokens to data and then save it in the database. So obviously having the database alter physical columns based off of their input to create anything would be a bit silly. So what they did is when you upload the CSV to the database, you can "map" these tokens you created and it will then serialize those tokens + date into an array and store it in the database in 1 column.

So when you read from the database, you call out 1 record and unserialize the array and work with it as a normal thing (like "a:2:{s:2:"kw";s:0:"";s:6:"status";s:2:"-1";}")

If it were up to me I'd have done things differently, but I'm taking over the code so I can't really change any fundamental stuff. They already have a system where they search for the product name (however I can't find where it's at or if they use it for the search or not) and they want to add the ability to filter through manufacturer as well.

so if the custom token+data they uploaded contains a manufacturer column in the array, to have only those records matching select out of the database.

So basically:

Upload CSV -> Map token (column) to data -> serialize -> store in 1 column in database matched to the set of data uploaded

Now to read out I have to:

Select records -> unserialize everything -> loop through arrays

Now obviously if I can do that **** in MySQL before pushing it to PHP, I won't end up with 50 million records having to process in PHP before I can have a set of 100 products display matching my search criteria...

So that's my question

How can i search on a serialized column within mysql using t-sql

t-sql (Transact-SQL) is Microsoft.

As for your search - best bet in this situation would be a LIKE search, or even better, a regular expression search, that way you can really extract the brand name more accurately than using LIKE.
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
t-sql (Transact-SQL) is Microsoft.

As for your search - best bet in this situation would be a LIKE search, or even better, a regular expression search, that way you can really extract the brand name more accurately than using LIKE.

So you'd go with a LIKE statement on a serialized array within the database

Thank you for "schooling" me on the right terminology I'm such a dumbass forgive my n00bness OMG I youtube'd the intarwebs can like to sniff my job like yes kkcyabye nn
 

DarkStreet

Expert Member
Joined
Jan 18, 2007
Messages
1,284
So you'd go with a LIKE statement on a serialized array within the database

Thank you for "schooling" me on the right terminology I'm such a dumbass forgive my n00bness OMG I youtube'd the intarwebs can like to sniff my job like yes kkcyabye nn

:erm:
 

Other Pineapple Smurf

Honorary Master
Joined
Jun 21, 2008
Messages
14,593
t-sql (Transact-SQL) is Microsoft.

As for your search - best bet in this situation would be a LIKE search, or even better, a regular expression search, that way you can really extract the brand name more accurately than using LIKE.

Agree as when you look at the serialized format you can select doing a:

SELECT * FROM x WHERE x.search_field ILIKE '%:"' || search_for_tag || '";' ;

This way you will always get the exact match. I would extend it to be case insensitive. Sorry my MySQL is not strong as I'm a PostgreSQL user. You might have to add escaping here.
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Thanks for everyone replying (even with my snotty remarks) but it appears I can't explain this **** to anyone without their eyes glazing over and they become drooling idiots. Maybe it's just my way of explaining that does it.

According to me LIKE won't be the most efficient way of doing a search in that DB of about 56 million records and it won't focus on on column (or token) within the serialized array, it will actually search the entire serialized array for "Sony" which could bring up false positives because some data capturer or "marketing genius" would think saying "This LG LCDTV is just like Sony only better" in the description of the product would actually show that as a positive hit and show up in the results when you're looking at only bringing up records with manufacturer = Sony.

I'm going to try and dumb it down into the columns of the database and the actual array (which can differ per database setup and hence why I can't really hard code this ****)

This is the main table I want to query:
ID
ProductName
42 " LCD TV with your mom's ass imprinted on the bevel because she's huge
Data
array('Manufacturer' => 'Sony', 'Ass Imprint By' => 'your mother','Description' => 'This is a TV, TVs are nice because its a TV, I like TV but I dont get to watch much of it because Im in a stupid ****ing profession that requires me to sit and figure out stupid ass clients ****ing needs and then implement some kind of ****ing solution without them spending a cent. **** you') <-- this is actually serialized and looks something like a:2:{s:2:"kw";s:0:"";s:6:"status";s:2:"-1";}
DateAdded


This is the table that holds the user-defined columns which is then mapped to the CSV file they import so that, even though the CSV file structure can vary wildly, they can bring order to the chaos and have one structure:

ID
TokenName
Manufacturer
Ass Imprint By
Description
DateAdded

This database then gets deployed at various locations/servers (client resells this application to other idiots)

So OTHER idiots are able to setup different Token Mappings (or column names) for the CSV import process

So now they want a "drop down" (****ing love it when a client wants EVERYTHING to be "drop downs") which contains a list of token (or column) mappings.

By default (as you might have noticed) searching by the product name is piss easy and straight-****ing-forward... but how do you search the serialized array without having to bring 56million records from mysql and then process each individually putting them in a list of their own and then outputting the 100 products Sony *might* have in the database?

Simple question
Is it even possible to search on a serialized array like that or should I tell me client he should pay for MS SQL Server 2008 which CAN do it...

And if it's possible *how* because IMO the point of a ****ing database is so you query it for a dataset and get specific records back.... otherwise we might as well just query select * from your_mothers_ass and drop the whole ****ing stupid "where" clause because "who needs it, we'll do it in PHP ah-duhr"


(yes I sound condescending and sarcastic but **** me I'm frustrated to hell and gone)

DO YOU GET WHAT IM SAYING NOW? OR DO I HAVE TO TRANSLATE THIS IN PHEDI PERHAPS?!
 

DarkStreet

Expert Member
Joined
Jan 18, 2007
Messages
1,284
Which part of this link didn't you understand? http://dev.mysql.com/doc/refman/5.1/en/regexp.html - using a well designed regular expression in your query you can easily obtain records matching the manufacturer.

Also, the guy who designed this database is a ****ing idiot who doesn't know the first thing about database design, I'm not surprised you're frustrated.
 

Drake2007

Expert Member
Joined
Oct 23, 2008
Messages
4,413
Have you considered splitting up that serialized array field out into a more usable temporary table using a transaction when it's updated etc. or is the array data just too dynamic for that?
 
Last edited:

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Which part of this link didn't you understand? http://dev.mysql.com/doc/refman/5.1/en/regexp.html - using a well designed regular expression in your query you can easily obtain records matching the manufacturer.

Also, the guy who designed this database is a ****ing idiot who doesn't know the first thing about database design, I'm not surprised you're frustrated.

Agreed and yes I'm looking at regular expressions ... didn't mention I was following the links/reading up/trying them out soz... needed to vent because **** ME HOW DO THESE TYPE OF PROGRAMMERS MAKE ANY ****ING MONEY!!?! I've seen a trend where the ***ker the programmer the more he ****s up and needs to fix his ****ups the more it builds and culminates till he just moves to another job and leaves whoever employed him high and dry... how the hell can you run something like that??

and @Drake

no, I'm trying to complicate things LESS
 
Top