无法使用PHP更新MS SQL日期时间字段

时间:2014-11-10 10:49:15

标签: php sql-server datetime

我正在尝试使用以下PHP代码更新MSSQL Server(2012)中的DATETIME字段:

$currentDate = date('Y-m-d H:i:s');
            $query = "  UPDATE Members
                        SET lastNotificationCheck = '?'
                        WHERE memberID=?;";
            $stmt = sqlsrv_prepare($this->con, $query, array(&$currentDate, &$_SESSION['member_id']));
            $execute = sqlsrv_execute($stmt);

            if ($execute === false) {
                die( print_r( sqlsrv_errors(), true));

                return false;
            } else {
                return true;
            }

以上是返回以下错误:

Array ( [0] => Array ( [0] => 22018 [SQLSTATE] => 22018 [1] => 245 [code] => 245 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Conversion failed when converting the varchar value '2014-11-10 11:30:35' to data type int. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Conversion failed when converting the varchar value '2014-11-10 11:30:35' to data type int. ) )

奇怪的是(我认为)以下查询在SQL Server Management Studio中完美运行:

UPDATE Members
SET lastNotificationCheck = '2014-11-10 11:18:00'
WHERE memberID=1000004;
    enter code here

我试图以某种不同的方式生成日期(date()和new DateTime()),但是当通过PHP执行时查询总是失败。

会员表:

CREATE TABLE [dbo].[Members](
[memberID] [int] IDENTITY(1,1) NOT NULL,
[membername] [varchar](20) NOT NULL,
[email] [varchar](50) NOT NULL,
[password] [varchar](50) NOT NULL,
[joindate] [datetime] NOT NULL,
[lastlogin] [datetime] NULL,
[lastNotificationCheck] [datetime] NULL

我希望有人能看出可能出现的问题。

1 个答案:

答案 0 :(得分:0)

删除'?'中的引号,如果值为字符串,则会自动添加它们。如果您添加它们,结果将是''value''

$query = "  UPDATE Members
  SET lastNotificationCheck = ?
  WHERE memberID=?;";
$stmt = sqlsrv_prepare($this->con, $query, array(&$currentDate, &$_SESSION['member_id']));