在 PHP7 和 FreeTDS/MSSQL 中,如果使用参数绑定,汉字会损坏,
In PHP7 with FreeTDS/MSSQL, Chinese character got corrupted if parameter binding is used,
复制脚本
$dbh = new \PDO("dblib:host=xxx;dbname=test;charset=utf8","test","test");
$str1 = '中文测试';
// Snippet 1: With parameter binding
$sql = "INSERT INTO TEST (text) VALUES (:text)";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(':text',$str1); // or $stmt->bindParam(':text',$str1,PDO::PARAM_STR);
$stmt->execute();
// Snippet 2: Text in SQL
$sql = "INSERT INTO TEST (text) VALUES ('".$str1."')";
$dbh->exec($sql);
问题描述
在PHP7,
对于代码段 1 - 数据库中的字符最终损坏 (Mojibake)。
对于代码段 2 - 一切正常。
在 PHP 5.5 中,两个片段都按预期工作。
我们在将 PHP 5.5 升级到 PHP 7 时发现了这一点。
所以问题是为什么代码段 1 在 PHP 7 设置中不起作用?
其他信息
错误设置 (PHP 7)
Ubuntu: Linux 3.19.0-25-generic #26~14.04.1-Ubuntu
PHP (from APT): 7.0.8-0ubuntu0.16.04.2
with builtin pdo_dblib: 7.0.8-0ubuntu0.16.04.2
freetds (from APT): 0.91-6.1build1
设置良好 (PHP 5.5)
Ubuntu: Linux 3.19.0-25-generic #26~14.04.1-Ubuntu
php (from APT): 5.5.9-1ubuntu4.14
with builtin pdo_dblib: 1.0.1
freetds (from APT): 0.91-5
两个设置中的 freetds 配置
/etc/freetds.config (both PHP 5.5 and 7 installation)
-------
tds version = 8.0
好的,这实际上是一个已知错误 - https://bugs.php.net/bug.php?id=72414
并在 PHP 7.0.12.
中修复
复制脚本
$dbh = new \PDO("dblib:host=xxx;dbname=test;charset=utf8","test","test");
$str1 = '中文测试';
// Snippet 1: With parameter binding
$sql = "INSERT INTO TEST (text) VALUES (:text)";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(':text',$str1); // or $stmt->bindParam(':text',$str1,PDO::PARAM_STR);
$stmt->execute();
// Snippet 2: Text in SQL
$sql = "INSERT INTO TEST (text) VALUES ('".$str1."')";
$dbh->exec($sql);
问题描述
在PHP7,
对于代码段 1 - 数据库中的字符最终损坏 (Mojibake)。
对于代码段 2 - 一切正常。
在 PHP 5.5 中,两个片段都按预期工作。
我们在将 PHP 5.5 升级到 PHP 7 时发现了这一点。
所以问题是为什么代码段 1 在 PHP 7 设置中不起作用?
其他信息
错误设置 (PHP 7)
Ubuntu: Linux 3.19.0-25-generic #26~14.04.1-Ubuntu
PHP (from APT): 7.0.8-0ubuntu0.16.04.2
with builtin pdo_dblib: 7.0.8-0ubuntu0.16.04.2
freetds (from APT): 0.91-6.1build1
设置良好 (PHP 5.5)
Ubuntu: Linux 3.19.0-25-generic #26~14.04.1-Ubuntu
php (from APT): 5.5.9-1ubuntu4.14
with builtin pdo_dblib: 1.0.1
freetds (from APT): 0.91-5
两个设置中的 freetds 配置
/etc/freetds.config (both PHP 5.5 and 7 installation)
-------
tds version = 8.0
好的,这实际上是一个已知错误 - https://bugs.php.net/bug.php?id=72414 并在 PHP 7.0.12.
中修复