简体   繁体   English

日期时间字段的mssql_query()字段为空

[英]mssql_query() fields blank for datetime fields

Running php 5.2.5 on Windows Server 2003 (64 bit) and connecting to SQL Server 2008 在Windows Server 2003(64位)上运行php 5.2.5并连接到SQL Server 2008

Having issues getting datetime fields from SQL Server using mssql_query. 使用mssql_query从SQL Server获取日期时间字段时遇到问题。 Any datetime field that I query returns nothing. 我查询的任何datetime字段均不返回任何内容。 All other fields in the query are correct, but the datetime fields are blank. 查询中的所有其他字段均正确,但datetime字段为空。

We switched from PEAR DB to mssql_ functions recently and that was the cause of the issue. 我们最近从PEAR DB切换到了mssql_函数,这就是问题的原因。 Switching back to PEAR is not an option. 不能切换回PEAR。

I found this on php.net mssql_query page and it does work, but I've got hundreds of queries and I was hoping there was a solution that wouldn't require us updating every single query. 我在php.net mssql_query页面上找到了它,它确实起作用,但是我有成百上千个查询,我希望有一种不需要我们更新每个查询的解决方案。 Anyone have a better solution that would not involve rewriting every query? 任何人都有更好的解决方案,不会涉及重写每个查询吗?

"I was tripped up by the fact that mssql_query() returns values for 'datetime' columns in an unusual format. In order to get your values returned in the usual 'YYYY-MM-DD HH:MM:SS' format, you can use the CONVERT function in your query like so: “我为mssql_query()以一种不寻常的格式返回'datetime'列的值而感到震惊。为了以常规的'YYYY-MM-DD HH:MM:SS'格式返回您的值,您可以在查询中使用CONVERT函数,如下所示:

SELECT CONVERT(varchar, INVOICE_DATE, 121) AS INVOICE_DATE ...

where 'INVOICE_DATE' is the name of the 'datetime' column." 其中“ INVOICE_DATE”是“日期时间”列的名称。”

Ended up just doing some string replacement to work around this. 最后只是做一些字符串替换来解决此问题。

First I created an array of date fields that I could possibly select from the SQL Server DB I was working with. 首先,我创建了一个日期字段数组,可以从正在使用的SQL Server DB中进行选择。

We've got a DB Abstraction class we run every query through. 我们有一个DB Abstraction类,我们运行每个查询。

In the DB class I check to see if "FROM" was in the query. 在数据库类中,我检查查询中是否存在“ FROM”。 If it was I split the string on FROM to get the SELECT fields part Then I do a foreach on the array of fields and if that field exists in the string I replace the field with CONVERT(varchar, FIELD, 121) AS FIELD 如果是我在FROM上拆分了字符串以获取SELECT字段部分,那么我对字段数组进行了一次foreach,并且如果该字段存在于字符串中,我将该字段替换为CONVERT(varchar,FIELD,121)AS FIELD

Works great for me...doesn't work with SELECT * but we don't use any SELECT * queries so not a problem for us. 对我来说很好用...不适用于SELECT *,但我们不使用任何SELECT *查询,因此对我们来说不是问题。

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

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