简体   繁体   English

PHP7 PDO_DBLIB 唯一标识符

[英]PHP7 PDO_DBLIB uniqueidentifier

I'm currently using the pdo_dblib extension in my project.我目前在我的项目中使用pdo_dblib扩展。 When I use this under PHP 5.6 with FreeTDS Version 7.0 set in config.当我在PHP 5.6下使用它时,在config.设置了FreeTDS Version 7.0 config. It will give me the uniqueidentifier as string .它会给我作为string的 uniqueidentifier 。 When i use the same script under PHP 7.0.8 it just gives me out some garbled shit.当我在PHP 7.0.8下使用相同的脚本时, PHP 7.0.8会给我一些乱码。 I don't wanna cast it in the SQL or convert it later in PHP .我不想在SQL转换它或稍后在PHP转换它。 Because sometimes it can be a uniqueidentifier in the result and sometimes not.因为有时它可以是结果中的唯一标识符,有时则不是。 so i would need to go through the complete results to convert the uniqueidentifier.所以我需要通过完整的结果来转换唯一标识符。

So is there any solution to get in work on PHP 7.0.8 like it was under PHP 5.6 ?那么是否有任何解决方案可以像在PHP 5.6下一样在PHP 7.0.8上工作?

As explained in the pull request mentioned in Bojan's answer, the cause of this issue in PHP 7 is that you have to instruct PDO to convert the GUIDs from binary to a string.正如 Bojan 的回答中提到的拉取请求中所解释的那样,PHP 7 中出现此问题的原因是您必须指示 PDO 将 GUID 从二进制转换为字符串。

This should do it for anyone struggling with this.对于任何为此而苦苦挣扎的人来说,这都应该这样做。

    /** @var \PDO $pdo */
    $pdo->setAttribute(\PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, true);

It looks like the bug was introduced in some minor version of PHP7.看起来这个错误是在 PHP7 的某个小版本中引入的。

  • 7.0.4 works perfectly. 7.0.4 完美运行。 You can manually compile it with phpbrew.您可以使用 phpbrew 手动编译它。
  • 7.0.8 has this implementation currently broken. 7.0.8 目前已经破坏了这个实现。

There was already a bug report filed for this issue here: https://bugs.php.net/bug.php?id=72601这里已经有针对此问题的错误报告: https : //bugs.php.net/bug.php?id=72601

and a pull request for a potential fix: https://github.com/php/php-src/pull/2001以及潜在修复的拉取请求: https : //github.com/php/php-src/pull/2001

Add the following flag to your \\PDO instance将以下标志添加到您的 \\PDO 实例

/** @var \PDO $pdo */
$pdo->setAttribute(\PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM