I am trying to insert XML string into a MSSQL database from PHP. I get:
SQLSTATE[HY000]: General error: 102 Incorrect syntax near ‘ (a chunk of the xml follows).
The prepared statement is:
$stmt = $this->db->prepare(' INSERT INTO [System_XmlExchangeImport] ([Create_ID], [ImportCode], [InputXml]) VALUES (?, ?, ?); '); $result = $stmt->execute(array('ABCDEFGHKAJSDKLJ', $code, $xml));
The $xml
is a valid XML string. The same XML can be inserted OK using an identical statement in an SQL client app (sqlectron)
I have no idea what to do next.
Advertisement
Answer
The problem was caused by encoding. The XML was encoded with Windows-1250, but the database connection was opened with UTF-8 charset:
$connString = "dblib:version=7.0;host=$dbIp:$dbPort;dbname=$dbName;charset=UTF-8;"; $this->db = new PDO($connString, $dbUser, $dbPass);
After changing UTF-8
to WINDOWS-1250
in the DSN ($connString
):
$connString = "dblib:version=7.0;host=$dbIp:$dbPort;dbname=$dbName;charset=WINDOWS-1250;";
the XML can be inserted with no problem.