Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript SQL Server时间格式不等于客户端时间格式_Javascript_Php_Sql_Sql Server_Sqlsrv - Fatal编程技术网

Javascript SQL Server时间格式不等于客户端时间格式

Javascript SQL Server时间格式不等于客户端时间格式,javascript,php,sql,sql-server,sqlsrv,Javascript,Php,Sql,Sql Server,Sqlsrv,我试图用SQL Query将客户端时间格式(“14:00”)与SQL Server时间格式(“14:00:00.0000000”)进行比较,从技术上讲,这是不相等的,因为SQL Server时间格式有秒和毫秒,因此,我尝试在PHP脚本中将秒和毫秒连接到客户端时间格式,例如14:00。“:00.0000000”,但它并不等于SQL Server时间格式 我的PHP脚本: // I assume that the $_POST['pAppointmentTime'] variable has a v

我试图用SQL Query将客户端时间格式
(“14:00”)
与SQL Server时间格式
(“14:00:00.0000000”)
进行比较,从技术上讲,这是不相等的,因为SQL Server时间格式有秒和毫秒,因此,我尝试在PHP脚本中将秒和毫秒连接到客户端时间格式,例如
14:00。“:00.0000000”
,但它并不等于SQL Server时间格式

我的PHP脚本:

// I assume that the $_POST['pAppointmentTime'] variable has a value of 14:00.
$pAppointTime = $_POST['pAppointmentTime'];                                      
$pAppointTime = $pAppointTime.":00.0000000";  

// SQL query 
"SELECT * FROM appointment WHERE 
   pAppointment_Date = '".$pAppointSchedDate."' ".
   "AND pAppointment_Time='".$pAppointTime."'".
   " AND Department='".$selectedDept."';"

那么,如何将这两个与SQL查询和PHP脚本进行比较呢?

您在PHP脚本14:00中使用了错误的语法。“:00.0000000”


“双引号的使用应该是从头开始到结束”->“14:0:00。0000000”< /P> < P>我认为你可以考虑以下内容:

  • 不要连接字符串来构建语句,不要在语句中使用参数来防止可能的SQL注入问题
  • 使用明确的日期和时间格式将日期和时间值作为文本传递-
    yyyymmdd
    hh:mm:ss
  • 对于您的具体情况,您可以尝试使用显式强制转换比较
    日期
    时间
    值,并且只为
    时间
    值添加秒数
例如,这是您问题的可能解决方案:

<?php
// Connection info
$server = 'server\instance';
$database = 'database';
$uid = 'uid';
$pwd = 'pwd';

// Connection
try {
    $conn = new PDO("sqlsrv:server=$server;Database=$database", $uid, $pwd);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch( PDOException $e ) {
    die( "Error connecting to SQL Server".$e->getMessage());
}

// Parameters
$pAppointSchedDate = "date in yyyymmdd format";
$pAppointTime = $_POST['pAppointmentTime'];                                      
$pAppointTime = $pAppointTime.":00"; 
$selectedDept = "...";

// Statement
try {
    $sql = "
        SELECT * 
        FROM appointment 
        WHERE 
           CONVERT(date, pAppointment_Date) = CONVERT(date, ?) AND 
           CONVERT(time(0), pAppointment_Time) = CONVERT(time(0), ?) AND 
           Department = ? 
    ";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(1, $pAppointSchedDate, PDO::PARAM_STR);
    $stmt->bindParam(2, $pAppointTime, PDO::PARAM_STR);
    $stmt->bindParam(3, $selectedDept, PDO::PARAM_STR);
    $stmt->execute();
    while ($row = $stmt->fetch( PDO::FETCH_ASSOC) ){
        echo print_r($row, true)."<br>";
    }
} catch (PDOException $e) {
    die ("Error executing query. ".$e->getMessage());
}

// End
$stmt = null;
$conn = null;
?>  


您能发布您当前的尝试吗?PHP脚本和SQL语句?这里是我的PHP脚本
$pAppointTime=$\u post['pAppointmentTime'];$pAppointTime=$pAppointTime.“:00.0000000”
假设
$\u POST['pAppointmentTime']
变量的值为
14:00
。这里是我的sql查询
“SELECT*FROM appointment WHERE pAppointment_Date=”、$pAppointSchedDate.“、$pAppointTime=”、“$pAppointTime.”和Department=”、“$selectedDept.”;“
@zhorovs谢谢,您需要考虑的一点是:1)您正在连接字符串以构建语句,因此您可能对sql onjection开放。请在语句中使用参数(…您使用什么驱动程序连接到SQL Server);2)
papointment\u Time
列的数据类型是什么;3) 支持的时间字符串文字格式。我个人的建议是重写PHP脚本并使用参数化语句。我理解感谢您的建议,
pAppointment\u Time
的数据类型是Time(7)您使用什么驱动程序连接到SQL Server-SQL Server的PHP驱动程序(SQLSRV或PDO\u SQLSRV),ODBC还是其他什么?
14:00
是一个变量值,所以它看起来像是
$client\u-Side\u Time=14:00
,我将变量连接到秒和毫秒,就像这样
$client\u-Side\u-Time.:00.0000000“