0

I have started work on a project that someone else created, this is what he has done:
In a mySql table he has a TEXT field called "allData", in that field he has serialized records that look like this

a:13:{s:12:"currencyCode";s:3:"SEK";s:11:"senderEmail";s:18:”[email protected]";
s:4:"HASH";s:32:"ebdeb85d094cefd102b630fa9e69d9ca";}

(There's a lot more data but I cut it down by around 95% so it would not mess up this page)

Any idea how I can search the above data for senderEmail or HASH?

4
  • Are you trying to search out the email from multiple rows or do you just need to extract the data from one row? @Ryan Commented Sep 25, 2015 at 21:06
  • O would consider changing the table structure to have a filed for each information Commented Sep 25, 2015 at 21:07
  • 1
    If you want to search for those specific fields in the database, you'll be better off in the long run storing them in their own proper columns (perhaps additionally to the serialised data, though that'll depend on the overall design.) Otherwise you'll end up with not-entirely-great kludges. Certainly any other way will be slower, especially for TEXT fields with lots of data in them. Commented Sep 25, 2015 at 21:07
  • If it was just one row it wouldnt be a problem, there are a few hundred rows :( Commented Sep 25, 2015 at 21:15

2 Answers 2

3
SELECT * FROM table WHERE allData LIKE '%"[email protected]"%' AND allData LIKE '%"ebdeb85d094cefd102b630fa9e69d9ca"%' LIMIT 1;;

This example should allow you to search for a specific HASH and senderEmail in the table and return that entry if a match of both fields exist in the serialized data. This would be useful to put into a view so that it can be retrieved quickly and easily, and you of course can see where you would want to change the email and hash in the query. In this example it will return that exact entry.

More information:

MySQL Like Function

Search inside serialize data with MYSQL

Sign up to request clarification or add additional context in comments.

7 Comments

If it was just one row it wouldnt be a problem, there are a few hundred rows :(
@Ryan are you looking for a specific email or do you just want entries with that pattern in it?
Specific email or hash.
then let me modify my answer for you.
Thanks, selected your answer :) I think I'm going to try to convert that into normal fields though, this seems like a pain in the behind... any suggestions?
|
0

You can retrieve the data normally. Then, use unserialize to turn it into an array that you can search.

3 Comments

Any explanation for the downvote?
Not my down-vote, but: If you do that, you don't need a (relational) database as you'd transfer all the data "into" the php instance anyway.
There are hundreds of rows, and new ones being added nearly every day... just ding one lookup would take ages, wouldnt it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.