I have a serialized object stored in a mysql database in a column of type text saved via php.
If I double click the field in PHPMyAdmin to edit the value of the field inline, and then save the edit by clicking away from the edit box, the serialized data in the field is rendered corrupt somehow.
I get the following PHP error after doing this:
Notice: unserialize(): Error at offset 913 of 1951 bytes in /path/to/file.php on line 46
I am not even changing any of the data, as I am only clicking in to copy the data to the clipboard so it's not as though I'm introducing anything weird, or making an error with the syntax etc. I thought maybe some whitespace is getting added or some wierd character(s).
Is there a solution to this?
According to this question, in order to avoid errors you can try to base64 the output or serialize() before inserting it into the database:
$toDatabse = base64_encode(serialize($data)); // Save to database
$fromDatabase = unserialize(base64_decode($data)); //Getting Save Format
There is a bug in phpmyadmin that saves the '\n' of a serialized php string as line breaks. PHP serialized strings must not contain line breaks! If you want to safely modify it directly from phpmyadmin, make sure to replace the line breaks with '\n' string, this can easily be done from any text editor.
Related
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#test.de";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#test.de";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?
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));
Im reading an excel line by line and doing some php operations in the backend ,but when i reached a cell containing "\" it throwed an error
it has to match the cell value and backend value
cell value "test-123\/123a"
backend value "test123/123a"
i tried reading the cell value as this str_replace("\/","/",$cellvalue);
But error still persists
You can try php function stripslashes http://php.net/manual/en/function.stripslashes.php
serialize data is
a:8:{s:10:"First_Name";s:6:"harish";s:9:"Last_Name";s:5:"verma";
s:5:"Email";s:16:"harish#facebook.com";s:7:"Address";s:6:"jaipur";s:4:"City";s:6:"Jaipur";s:5:"State";
s:9:"Rajasthan";s:12:"Country_Name";s:5:"India";s:7:"Cell_No";s:10:"8787878787";}
it return true when we change Email to harish#gmail.com .... please help and thanks in advance.
If you notice in the serialized data you have this section defining the email address
s:16:"harish#facebook.com";
Thats says this field is a string of 16 characters. The string in the field is NOT 16 characters its 19 characters
So I guess you are manually fiddling with the data after is has been properly serialize()'d
If you are going to manually mess with the serialized data you also have to make it all add up. So either stop manually editing it and re serialize() the data properly OR remember to also change the size parameter data to match your edits.
Data that is stored in my mysql database, has the breaks of text input properly, but when retrieved the data no longer has the breaks, and everything is displayed as one string without any <br>. Any idea why this would occur?
The data column is the format text,
for example: in a table there is:
hey
how
do
you
do
After retrieving the data ill echo $mesarray[0][message]; and the result is:
hey how do you do
I want the line breaks to appear, but they dont.
The reason is because HTML does not understand line breaks. They need <br /> tags to break lines.
There is a function called nl2br() which can be used to convert new lines to <br>
echo nl2br($mesarray[0][message]);
I'm getting data from my textarea with the following code
$about_me=mysql_real_escape_string(nl2br($_POST['about_me']));
which
1. Receives data, using $_POST.
2. nl2br makes brakes so If I echo this code to user he will see if there were new lines.
3. mysql_real_escape_string to secure code from mysql injections before entering it to database.
So if I echo this code everything works fine.
But If I edit it again through textarea, php goes to mysql gets data, puts it to textarea and I see <br> signs...
How can I get rid of them while editing my text again in textarea ?
How can I get rid of them while editing my text again in textarea ?
Stop using nl2br(), of course. It's entirely wrong here.
You use nl2br() when you want to output data that contains linebreaks to HTML, not when you want to store it in the database. Store data unchanged, format it for viewing.
If you output it into a <textarea> you don't need to use it either, since textareas display linebreaks (whereas HTML in general does not). For the textarea you need htmlspecialchars(), but apparently this is already happening - otherwise you would not see literal <br> showing up.
<?php
function br2nl($string){
$return=eregi_replace('<br[[:space:]]*/?'.
'[[:space:]]*>',chr(13).chr(10),$string);
return $return;
}
?>
Use this while getting data from database and before printing into textarea .
http://php.net/manual/en/function.nl2br.php
Check examples on this page