使用 TS 准备好的语句
Prepared Statement with TS
我在使用 sqlsrv 驱动程序的语法时遇到问题。
这很好用:
$sql = "SELECT * from Table1 WHERE (Table1.Time >= {ts '2017-05-08
00:00:00' } AND Table1.Time < {ts '2017-05-10 00:00:00' })";
$stmt = sqlsrv_query( $conn, $sql );
$tbl = "<table>"
while ($row = sqlsrv_fetch_array($stmt)){
$tbl .= "<tr><td>".$row[0]."</td><td>".$row[1]->format('Y-m-d H:i:s')."</td><td align=\"center\">".$row[2]."</td></tr>";
}
$tbl .= "</table>";
echo $tbl;
但这并不是全部:
$sql = "SELECT * from Table1 WHERE (Table1.Time >= {ts ? } AND Table1.Time < {ts ? })";
$stmt = sqlsrv_prepare( $conn, $sql, array('2017-05-08 00:00:00','2017-05-10 00:00:00'));
$result = sqlsrv_execute($stmt);
$tbl = "<table>"
while ($row = sqlsrv_fetch_array($stmt)){
$tbl .= "<tr><td>".$row[0]."</td><td>".$row[1]->format('Y-m-d H:i:s')."</td><td align=\"center\">".$row[2]."</td></tr>";
}
$tbl .= "</table>";
echo $tbl;
我收到此错误:
An invalid parameter was passed to sqlsrv_execute. [message] => An invalid parameter was passed to sqlsrv_execute. )
是否有将日期变量传递给 { ts ? }
语句的正确方法?
您不太可能自己使用 ODBC escape sequences. Unlike MySQL (which has a UNIX_TIMESTAMP
function) there are no functions built in to SQL Server that will convert to a Unix timestamp for you. You could 获得准备好的语句,但也许在 PHP 中更容易做到:
$begin = strtotime('2017-05-08 00:00:00');
$end = strtotime('2017-05-10 00:00:00');
$sql = "SELECT * FROM Table1 WHERE Table1.Time >= ? AND Table1.Time < ?)";
$stmt = sqlsrv_prepare($conn, $sql, array($begin, $end));
$result = sqlsrv_execute($stmt);
$tbl = "<table>"
while ($row = sqlsrv_fetch_array($stmt)){
$row[1] = $row[1]->format('Y-m-d H:i:s');
$tbl .= "<tr><td>$row[0]</td><td>$row[1]</td><td align=\"center\">$row[2]</td></tr>";
}
$tbl .= "</table>";
echo $tbl;
您应该枚举要获取的列而不是使用 *
,然后按名称(例如 $row["Time"]
)而不是数字引用它们。它将使您的代码更具可读性,并保护它免受数据库模式更改的影响。
我在使用 sqlsrv 驱动程序的语法时遇到问题。
这很好用:
$sql = "SELECT * from Table1 WHERE (Table1.Time >= {ts '2017-05-08
00:00:00' } AND Table1.Time < {ts '2017-05-10 00:00:00' })";
$stmt = sqlsrv_query( $conn, $sql );
$tbl = "<table>"
while ($row = sqlsrv_fetch_array($stmt)){
$tbl .= "<tr><td>".$row[0]."</td><td>".$row[1]->format('Y-m-d H:i:s')."</td><td align=\"center\">".$row[2]."</td></tr>";
}
$tbl .= "</table>";
echo $tbl;
但这并不是全部:
$sql = "SELECT * from Table1 WHERE (Table1.Time >= {ts ? } AND Table1.Time < {ts ? })";
$stmt = sqlsrv_prepare( $conn, $sql, array('2017-05-08 00:00:00','2017-05-10 00:00:00'));
$result = sqlsrv_execute($stmt);
$tbl = "<table>"
while ($row = sqlsrv_fetch_array($stmt)){
$tbl .= "<tr><td>".$row[0]."</td><td>".$row[1]->format('Y-m-d H:i:s')."</td><td align=\"center\">".$row[2]."</td></tr>";
}
$tbl .= "</table>";
echo $tbl;
我收到此错误:
An invalid parameter was passed to sqlsrv_execute. [message] => An invalid parameter was passed to sqlsrv_execute. )
是否有将日期变量传递给 { ts ? }
语句的正确方法?
您不太可能自己使用 ODBC escape sequences. Unlike MySQL (which has a UNIX_TIMESTAMP
function) there are no functions built in to SQL Server that will convert to a Unix timestamp for you. You could
$begin = strtotime('2017-05-08 00:00:00');
$end = strtotime('2017-05-10 00:00:00');
$sql = "SELECT * FROM Table1 WHERE Table1.Time >= ? AND Table1.Time < ?)";
$stmt = sqlsrv_prepare($conn, $sql, array($begin, $end));
$result = sqlsrv_execute($stmt);
$tbl = "<table>"
while ($row = sqlsrv_fetch_array($stmt)){
$row[1] = $row[1]->format('Y-m-d H:i:s');
$tbl .= "<tr><td>$row[0]</td><td>$row[1]</td><td align=\"center\">$row[2]</td></tr>";
}
$tbl .= "</table>";
echo $tbl;
您应该枚举要获取的列而不是使用 *
,然后按名称(例如 $row["Time"]
)而不是数字引用它们。它将使您的代码更具可读性,并保护它免受数据库模式更改的影响。