Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

serialze problem

Status
Not open for further replies.

lonelydragon

Programmer
Aug 5, 2004
75
US
thanks for help:
i am now serialize and unserialize an array. and in the array , there are some single quote. when serialize the array, i use addslashes because i need to insert the data to the database. to this point everything ie ok. but when i try to unserialze the data from the database, i got an error because the data has some single quote there. any one know how to solve this problem? thanks in billion.

dragon
 
You can use stripslashes() to remove the slashes added with addslashes().

The better way is to use mysql_real_escape_string() when you put it in the database. You don't have to do anythine when you remove it, since MySQL will automagically do the right thing.

Ken
 
thank you for you reply. actually. the point is there are some single quotes in the serialied data. the problem is not "insert" but "retrieve". when you unserialize it, it will be false. you can just try yourself.

and now i replace the single quote with some letters when i serialize the data. and change back "after" unserialize the data. if you have a better solution, please let me know . thanks
 
Please post examples of the data before serialization, after, and after unserializing it.
 
first, addslashes and insert $test2 to database,
$test1 = array("me","he","dragon's");
$test2 = serialize($test1);
notice the single quote here.

and then, retrieve the serialized data from the database.
$newData= data retrieved from database(addslashed and serialised);

$test3 = unserialize(stripslashes($newData)));

and here, $test3 is FALSE;
 
This works fine, I didn't store it in the database.
Code:
$test1 = array("me","he","dragon's");
$test2 = serialize($test1);
echo $test2;
$test3 = addslashes($test2);
echo '<br>'.$test3;
$test4 = unserialize(stripslashes($test3));
echo '<pre>';print_r($test4);echo '</pre>';

Are you doing something different? Besides putting it into the DB and taking it out?

Ken
 
yes,
you need to insert it into database. that is the key.
dragon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top