I am trying to insert ASCII NUL characters ( \0aka U+0000) into a SQL Server database with PHP using pdo_sqlsrv. This is a requirement for handling serialized PHP strings that contain NUL characters to represent private / protected variables.
However, there is something about PDO :: quote () that breaks the lines.
Code to play (replace DBNAME, USERNAMEand PASSWORDwith the appropriate values):
<?php
try {
$dsn = 'sqlsrv:Server=.\SQLEXPRESS;Database=DBNAME';
$user = 'USERNAME';
$pass = 'PASSWORD';
$connection = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
die("Connection error: " . $e->getMessage());
}
$str = "XX\0XX";
header("Content-Type: text/plain");
print("Original: " . str_replace("\0", "{NUL}", $str) . "\n");
$str = $connection->quote($str);
print("Quoted: " . str_replace("\0", "{NUL}", $str) . "\n");
?>
Expected Result:
Original: XX{NUL}XX
Quoted: 'XX{NUL}XX'
Actual output:
Original: XX{NUL}XX
Quoted: 'XX'{NUL}{NUL}a
Can someone explain this strange behavior and, more importantly, explain how to solve it?
UPDATE
, , e. , . . , pdo_sqlsrv?