【问题标题】:PHP different result of getttype() on apache and IISapache和IIS上getttype()的PHP不同结果
【发布时间】:2019-04-17 13:41:02
【问题描述】:

生产服务器是:

  • Windows 10 专业版
  • IIS 10.0.17134.1
  • PHP 版本 7.1.17

我的本​​地机器:

  • macOS,莫哈韦 10.14
  • Apache/2.4.37 (Unix)
  • PHP 版本 7.2.12

在两台机器上,我都使用相同的 laravel 5.4 应用程序,该应用程序在同一 Microsoft SQL 服务器上使用相同的源(数据库),并使用遵循此 link 安装的 sqlsrv PDO。

reports表中,is_active字段是tinyint类型。

在windows机器上:

  $report = App\Report::find(1);

  return gettype($report->is_active); //returns string

在 macOS 机器上:

  $report = App\Report::find(1);

  return gettype($report->is_active); //returns integer

因此,当我在本地主机上进行开发并将更改推送到生产环境时,它在基本比较中失败了:

 if($report->is_active === 1){
       // works on localhost, but not on production
 }

【问题讨论】:

  • 只需在比较中去掉第三个等号。或者在返回时将值转换为 int。 if($report->is_active == 1){
  • 查看PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE 或自己转换每个字符串。

标签: php laravel apache iis


【解决方案1】:

正如评论中提到的Salaman A,请看PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE

在生产服务器上,我在 database.php 文件中添加了options 数组。

  'sqlsrv' => [
        // defaults ...
        'options' => [
            PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true
        ]
    ],

这解决了我的问题,现在在两台机器上gettype($report->is_active) 返回integer

【讨论】:

    猜你喜欢
    • 2015-12-22
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    相关资源
    最近更新 更多