tags:

views:

22

answers:

2

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 )

A: 

What happens when you define it as a string specifically?

$stmt->bindParam(':condition',$condition,PDO::PARAM_STR);
Alec
It gives exactly the same error message
SteD
+1  A: 

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.

Ondrej Slinták