doupan6648 2014-05-14 09:42
浏览 32
已采纳

来自MS-SQL的SmallDateTime无法在PHP中运行

I have a strange problem that when using PHP to connect to a MSSQL database using freetds the query returns no data.

I have two SQL queries:

SELECT TOP 100 *
FROM [TBL_Contact] AS C
LEFT JOIN [TBL_Address] AS A
    ON (C.[ContactID] = A.[ContactID])
;

and

SELECT TOP 100
    N.[NoteID],
    N.[NoteTypeID],
    N.[IsPrivate],
    CAST(N.[NoteText] AS TEXT) AS [NoteText],
    N.[ManageUserID],
    N.[DisplayDate],
    N.[CreateUserID],
    N.[CreateDateE],
    N.[EditUserID],
    N.[EditDate]
FROM [TBL_Note] AS N
LEFT JOIN [TBL_Contact_Note] AS C
    ON (C.[NoteID] = N.[NoteID])
;

When run directly on the MSSQL Server they both work as expected and return data. However when running them via PHP by wrapping them in a mssql_query() function call.

$result = mssql_query('
   SELECT TOP 100 *
   FROM [TBL_Contact] AS C
    LEFT JOIN [TBL_Address] AS A
        ON (C.[ContactID] = A.[ContactID])
    ;
');

$result1 = mssql_query('
    SELECT TOP 100
        N.[NoteID],
        N.[NoteTypeID],
        N.[IsPrivate],
        CAST(N.[NoteText] AS TEXT) AS [NoteText],
        N.[ManageUserID],
        N.[DisplayDate],
        N.[CreateUserID],
        N.[CreateDateE],
        N.[EditUserID],
        N.[EditDate]
    FROM [TBL_Note] AS N
    LEFT JOIN [TBL_Contact_Note] AS C
        ON (C.[NoteID] = N.[NoteID])
    ;
');

Then only the first one returns data.


I have experimented by removing fields from the query and have found that it is the smalldatetime field N.[DisplayDate] that is causing the issue. When I run the query with that field removed then the mssql_query() call works.

  • 写回答

1条回答 默认 最新

  • dongzaliang4492 2014-05-15 10:59
    关注

    Ok I've found out what the issue is there is a bug with PHP and the smalldatetime data type

    There is a fix for this which involves disabling the mssql.datetimeconvert setting in the php.ini file.

    The fix found on the web is to using ini_set to change the setting.

    ini_set('mssql.datetimeconvert', 0);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)