2

I have the following problem. I am retrieving a mysql text field that is a serialized text of a invoice. I am working in 2 different projects. Both have the same version of PHP. The data was exported & imported from db to db. If i var_dump the data from db1 it tells me it's length is x. When I do the same in from db2 i get x+2

string(595) "a:3:{s:11:"userdetails";a:20:{s:4:"name";s:3:"bas";s:8:"lastname";s:7:"schmitz";s:5:"email";s:17:"[email protected]";s:6:"street";s:11:"f�rstenwall";s:7:"street2";s:0:"";s:7:"company";s:0:"";s:3:"zip";s:5:"40215";s:9:"residence";s:10:"d�sseldorf";s:7:"country";s:7:"Germany";s:5:"phone";s:7:"3033185";s:3:"fax";s:0:"";s:10:"customerID";i:202771;s:2:"nr";s:3:"228";s:6:"region";s:3:"nrw";s:10:"phone_code";s:3:"211";s:8:"fax_code";s:0:"";s:10:"salutation";s:2:"Mr";s:5:"sales";s:0:"";s:12:"country_code";s:0:"";s:10:"vat_number";s:0:"";}s:6:"domain";s:15:"bas-schmitz2.de";s:10:"has_domain";b:1;}"


string(597) "a:3:{s:11:"userdetails";a:20:{s:4:"name";s:3:"bas";s:8:"lastname";s:7:"schmitz";s:5:"email";s:17:"[email protected]";s:6:"street";s:11:"fürstenwall";s:7:"street2";s:0:"";s:7:"company";s:0:"";s:3:"zip";s:5:"40215";s:9:"residence";s:10:"düsseldorf";s:7:"country";s:7:"Germany";s:5:"phone";s:7:"3033185";s:3:"fax";s:0:"";s:10:"customerID";i:202771;s:2:"nr";s:3:"228";s:6:"region";s:3:"nrw";s:10:"phone_code";s:3:"211";s:8:"fax_code";s:0:"";s:10:"salutation";s:2:"Mr";s:5:"sales";s:0:"";s:12:"country_code";s:0:"";s:10:"vat_number";s:0:"";}s:6:"domain";s:15:"bas-schmitz2.de";s:10:"has_domain";b:1;}"

As I am pasting these I can see that there is a difference when displaying germanic characters

Any idea to why this is happening?

4
  • Do you have same charset/encoding in both databases? Commented Jun 19, 2017 at 6:53
  • latin1_swedish_ci Commented Jun 19, 2017 at 8:43
  • Why do you use swedish charset and expect to get the correct results? Use utf8 (or even better, utf8mb4) to prevent things like this. Also make sure you output utf8 and your files have correct charset set as well. Commented Jun 20, 2017 at 8:03
  • @user1840302 Your serialized string are corrupted. For instance, the string length for [email protected] is 13 but your serialized string has stated 17. Thus, PHP is unable to unserialize the corrupted string. Commented Jun 20, 2017 at 8:20

1 Answer 1

1

The output of serialize() cannot be handled as plain text:

Return Values

Returns a string containing a byte-stream representation of value that can be stored anywhere.

Note that this is a binary string which may include null bytes, and needs to be stored and handled as such. For example, serialize() output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field.

Thus your data is corrupted in the first place.

If you're unable to change the database design (which would be the proper fix), you need to re-encode serialised data in a plain text encoding such as Base64:

$encoded = base64_encode(serialize($foo));
$decoded = unserialize(base64_decode($encoded));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.