SteD
SteD

Reputation: 14025

PDO prepare Insert serialize data error

I am trying to insert a serialize data into mySQL using PDO and I'm hitting some syntax error. Have I missed out something?

Some simplified coding:

$test['1'] = "one";
$condition = serialize($test);
$stmt = $dbh->prepare("INSERT INTO weather(condition) VALUES (:condition)");
$stmt->bindParam(":condition",$condition);
$stmt->execute();

$stmt->debugDumpParams() shows

SQL: [67] INSERT INTO weather(condition)

VALUES (:condition) Params: 1 Key: Name: [10] :condition paramno=-1 name=[10] ":condition" is_param=1 param_type=2

print_r($stmt->errorInfo()) shows

Array ( [0] => 42000 [1] => 1064 [2] => 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 'condition) VALUES ('a:1:{i:1;s:3:\"one\";}')' at line 1 )

Upvotes: 1

Views: 1118

Answers (2)

Ondrej Slinták
Ondrej Slinták

Reputation: 31940

Solution is in fact very simple. Condition is reserved word in MySQL. You cannot use it as name of your column.

You can find full list of reserved words for MySQL on their webpage here and here.

Upvotes: 2

Alec
Alec

Reputation: 9078

What happens when you define it as a string specifically?

$stmt->bindParam(':condition',$condition,PDO::PARAM_STR);

Upvotes: 0

Related Questions