idontknowhow
idontknowhow

Reputation: 1527

can't save serialized data into MySQL

Here what I have on my code I dont see what is wrong on my code but I can't figured it out why it wont save on database.

My Array Details

Array
(
    [dates] => Array
        (
            [0] => 2012-01-18
            [1] => 2012-02-18
            [2] => 2012-03-18
            [3] => 2012-04-18
            [4] => 2012-05-18
        )

    [amount] => Array
        (
            [0] => 2000
            [1] => 2000
            [2] => 2000
            [3] => 2000
            [4] => 2000
        )

    [deposit] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

)

My saving code

$sql = "INSERT INTO transaction (id, details)
        VALUES('$id', $serializeddetails)";

$query = mysql_query($sql) or die("Fatal error: ".mysql_error());

and I got this error

Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a:3:{s:5:"dates";a:6:{i:0;s:10:"2012-01-18";i:1;s:10:"2012-02-18";i:2;s:10:"2012' at line 2

I used longtext for my type of data and try longblob but no hope,

I try also use also base64_ecode();

$serializeddetails = base64_encode(serialize($detailsarray));

and got this error

Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 2

what did I something wrong on my code why this wont save into database?

thank you

Upvotes: 0

Views: 4162

Answers (1)

Eugen Rieck
Eugen Rieck

Reputation: 65342

$sql = "INSERT INTO transaction (id, details)
        VALUES('$id', '$serializeddetails')";

$serializeddetails is a string, sou you need quotes. Also make sure, the serialized data is escaped. mysql_real_escape_string() or addslashes() comes to mind. This shouldn't be necessary with your test data (contains no single quotes after serialization), but it can be with other data.

Upvotes: 2

Related Questions