Miley2 Miley2 -4 years ago 72
MySQL Question

How can I transfer a serialize array to a database in PDO?

it is already a few days that I am looking for this to know :

How can I transfer a serialize array to a database in PDO ?

My data bases:

id | name | surname


My function which does not work:

public function save($value2, $value3)
{
$array = array('id' => '', ‘name’ => "$value2", ‘surname’ => "$value3");
$serialize = serialize($array);

mysqli_query("INSERT INTO mytable VALUES(NULL, 'serialize', '".mysqli_real_escape_string($serialize)."')") or die(mysqli_error());

}


For information, my $value1 is null because I use the auto increment in my table.

...

Thank you for your help :)

Answer Source

Since you’re supposed to be using PDO, you should finish the job that way. While you’re there use pepared statements which will handle all of the tricky parts, such as strings which break SQL.

Assuming that you already have a PDO object $pdo, this gives you:

$prepared=$pdo->prepare('INSERT INTO mytable VALUES(NULL,?,?)');
$prepared->execute(array('serialize',$serialize));

You can also use prepared statements if you’re using mysqli_ statements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download