简体   繁体   English

从字符串转换日期和/或时间时,PHP mssql_query错误

[英]PHP mssql_query errors when converting date and/or time from character string

I am using PHP to query some data on SQL Server 2008 R2 and receiving the following errors: 我正在使用PHP查询SQL Server 2008 R2上的某些数据并收到以下错误:

  • PHP Warning: mssql_query(): message: Conversion failed when converting date and/or time from character string. PHP警告:mssql_query():消息:从字符串转换日期和/或时间时转换失败。 (severity 16) in /var/www/html/BSC_Entry.php on line 14 (严重性16)在第14行的/var/www/html/BSC_Entry.php中
  • PHP Warning: mssql_query(): General SQL Server error: Check messages from the SQL Server (severity 16) in /var/www/html/BSC_Entry.php on line 14 PHP警告:mssql_query():常规SQL Server错误:在第14行的/var/www/html/BSC_Entry.php中检查来自SQL Server(严重性16)的消息

Here's the code block: 这是代码块:

  3 <?php // Initialise all database actions
  4     //IP of server
  5     $server = 'x.x.x.x';
  6 
  7     // Connection to MSSQL - possibly use password file
  8     $link = mssql_connect($server, 'user', 'pass');
  9     if (!$link) {
 10         die('Something went wrong while connecting to MSSQL');
 11     }
 12 
 13     // Declare query to return BSC_Name, BSC_Owner and  
 14     $qBSCInfo = mssql_query('SELECT * FROM dbo.BSC_Info;', $link);
 15 
 16 ?>

Initially, I was passing the SQL below as a parameter to mssql_query, but after receiving the errors, created the database view above 'BSC_Info' (with appropriate permissions) in case the query was too complex for mssql_query to handle: 最初,我将下面的SQL作为参数传递给mssql_query,但是在收到错误后,在查询“ BSC_Info”上方创建了数据库视图(具有适当的权限),以防查询过于复杂而使mssql_query无法处理:

SELECT DISTINCT 
    dbo.BSC.BSC_ID,
    dbo.BSC.BSC_Name, 
    dbo.BSC.BSC_Owner, 
    DATEDIFF(M, MAX(CONVERT(DATETIME, LEFT(dbo.BSCDataSet.DatePeriod, 4) 
                      + RIGHT(dbo.BSCDataSet.DatePeriod, 2) + '01')), CONVERT(DATETIME, LEFT(CONVERT(VARCHAR, GETDATE(), 120), 4) + RIGHT(LEFT(CONVERT(VARCHAR, GETDATE(), 
                      120), 7), 2) + '01')) AS Periods_to_Current
FROM dbo.BSC 
LEFT OUTER JOIN dbo.BSCDataSet 
ON dbo.BSC.BSC_ID = dbo.BSCDataSet.BSC_ID
GROUP BY dbo.BSC.BSC_ID, dbo.BSC.BSC_Name, dbo.BSC.BSC_Owner

To clarify, the query works in SQL Server Management Studio returning some fields from a table along with the difference (in months) between the current date and an earlier date (stored in the database as a VARCHAR - YYYYMM format). 为了澄清起见,该查询在SQL Server Management Studio中起作用,它从表中返回一些字段以及当前日期和较早日期(以VARCHAR-YYYYMM格式存储在数据库中)之间的差异(以月为单位)。 In order to prevent any issues with partial months, I've set the compared dates to the first day of the month. 为了防止部分月份出现任何问题,我将比较日期设置为该月的第一天。 I am sure there's a more graceful way of doing this, but I have very little SQL Server experience, or PHP for that matter! 我敢肯定有一种更优雅的方法可以做到这一点,但是我几乎没有SQL Server经验,或者说PHP也没有!

Data types are: 数据类型为:

  • BSC_ID - numeric(5,0) BSC_ID-数字(5,0)
  • BSC_Name - varchar(50) BSC_Name-varchar(50)
  • BSC_Owner - varchar(50) BSC_Owner-varchar(50)
  • Periods_to_Current - int Periods_to_Current-int

Any help would be much appreciated. 任何帮助将非常感激。 Cheers all! 欢呼大家!

So there were a number of issues with this code: 因此,此代码存在许多问题:

  • php.ini file needed the mssql setting: mssql.datetimeconvert = 0 php.ini文件需要mssql设置: mssql.datetimeconvert = 0
  • Any CONVERT-ed date fields needed the date format 121 任何转换日期字段都需要日期格式121
  • the original code tried to perform conversions, date arithmetic and MAX() functions on a potentially NULL date field - oops 原始代码试图对可能为NULL的日期字段执行转换,日期算术和MAX()函数-oops

I have now amended the code to a more appropriate (and working): 我现在将代码修改为更合适的代码(并且可以正常工作):

SELECT 
  dbo.BSC.BSC_ID,
  dbo.BSC.BSC_Name, 
  dbo.BSC.BSC_Owner, 
  CASE maxview.maxDate 
    WHEN NULL THEN NULL 
    ELSE DATEDIFF(M,CONVERT(DATETIME, LEFT(maxview.maxdate, 4)+ RIGHT(maxview.maxdate, 2) + '01', 121),GETDATE()) 
  END 
FROM dbo.BSC 
LEFT OUTER JOIN (SELECT BSC_ID, MAX(dbo.BSCDataSet.DatePeriod) as maxDate 
FROM dbo.BSCDataSet GROUP BY BSC_ID) maxview 
ON dbo.BSC.BSC_ID = maxview.BSC_ID;

Hopefully this will help someone out! 希望这可以帮助某人!

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

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