0

如何将日期传递给 PHP 和 MS SQL 的占位符?

我正在使用的示例代码:

$fromDate = '2013-01-01';
$toDate = '2013-02-28';
$empno = 12345;

$sqlStr = "SELECT * FROM histTable WHERE EmpNum = ?
           AND toDate >= ? AND fromDate <= ?";

$sqlResult = $this->db->query($sqlStr, $empno, $fromDate, $toDate);

除非我对日期值进行硬编码,否则我无法从数据库中获取任何信息。

有人可以帮忙吗?

谢谢你。

4

3 回答 3

0

它全部在''或“”中,我宁愿使用“”;

$fromDate = '2013-01-01';
$toDate = '2013-02-28';
$empno = 12345;

$sqlStr = "SELECT * FROM histTable WHERE EmpNum = ?
       AND toDate >= '{$fromDate}' AND fromDate <= '{$toDate}' ";

$sqlResult = $this->db->query($sqlStr, $empno, $fromDate, $toDate);

注意 vars 周围的逗号。

于 2013-02-14T13:06:57.883 回答
0

直接离开我的头顶,尝试改变:

$sqlStr = "SELECT * FROM histTable WHERE EmpNum = ? AND toDate >= ? AND fromDate <= ?";

$sqlStr = "SELECT * FROM histTable WHERE EmpNum = ? AND toDate >= '?' AND fromDate <= '?'";
于 2013-02-14T13:02:34.557 回答
0

尝试这个 ...

$fromDate = '2013-01-01 00:00:00';
$toDate = '2013-02-28 00:00:00';
$empno = 12345;

$sqlStr = "SELECT * FROM histTable WHERE EmpNum = $empno
           AND toDate >= '$fromDate' AND fromDate <= '$toDate'";

$sqlResult = $this->db->query($sqlStr);
于 2013-02-14T12:52:15.550 回答